简体   繁体   English

如何根据所选的选择器值显示组件? [反应原生]

[英]how to show a component depending of picker value selected? [React native]

I'm trying to make a game with react native and I want to show a different options when i change the picker value. 我正在尝试制作一个具有本机反应的游戏,并且当我更改选择器值时,我想显示一个不同的选项。 basically when I select the first option on the picker a component has to appear and when I select the second one another component. 基本上,当我在选择器上选择第一个选项时,必须出现一个组件,而当我选择第二个另一个组件时,则必须出现。

I tried this function but not working 我尝试了此功能但不起作用

pickerOptionText = () => {
  if (this.state.PickerValueHolder==this.state.filter[0]) {
    return (
      <Text>{instructions[2]}</Text>
    );
  }else {
    return (
      <Text>{instructions[1]}</Text>
    );
  }
  return null;
}

here is my code 这是我的代码

export default class Facil extends Component {

   constructor(props)
   {
     super(props);
     this.state = {
     isLoading: true,
     PickerValueHolder : '',
     filter: [
       {
         "option":"Palabras por categoria"
       },
       {
         "option":"Palabras por caracteres"
       }
     ],
     dataSource:[]
    }
   }
   componentDidMount() {

        return fetch(API_URL)
          .then((response) => response.json())
          .then((responseJson) => {
            this.setState({
              isLoading: false,
              dataSource: responseJson
            })
          })
          .catch((error) => {
            console.error(error);
          });
      }

  render() {
    const resizeMode = 'stretch';


pickerOptionText = () => {
  if (this.state.PickerValueHolder==this.state.filter[0]) {
    return (
      <Text>{instructions[2]}</Text>
    );
  }else {
    return (
      <Text>{instructions[1]}</Text>
    );
  }
  return null;
}



    return (
      <View style={styles.container}>
        <Image source={require('../../Images/HomeLayout.png')}
          style={styles.imagen}
        />
      <View style={styles.mView}>
        <View style={styles.panel}>
          <Text style={styles.titlePanel}>MODO FACIL</Text>
          <Text style={styles.instructions}>{instructions[0]}</Text>
          <View style={styles.picker}>
          <Picker
            selectedValue={this.state.PickerValueHolder}
            style={ {height: '100%',width: '100%'}}
            mode="dropdown"
            onValueChange={(itemValue, itemIndex) => this.setState({PickerValueHolder: itemValue})} >
            { this.state.filter.map((item, key)=>(
              <Picker.Item label={item.option} value={item.option} key={key} />)
              )}
            </Picker>
          </View>

          <View style={styles.gameOpt}>
            <Text>[dynamic options]</Text>
            {pickerOptionText}
          </View>

        </View>
      </View>
      <TouchableOpacity style={styles.button}><Text style={styles.btnText}>Play!</Text></TouchableOpacity>
      </View>
    );
  }
}

You forgot '()'. 你忘了 '()'。 pickerOptionText is a function, not a React component. pickerOptionText是一个函数,而不是一个React组件。

<Text>[dynamic options]</Text>
{pickerOptionText}

to: 至:

<Text>[dynamic options]</Text>
{pickerOptionText()}

You can try using Conditional Rendering of JSX, by this you can use ternary operator and a simple if condition. 您可以尝试使用JSX的条件渲染,从而可以使用三元运算符和简单的if条件。 this is written as: 这写为:

{this.state.PickerValueHolder==this.state.filter[0] ? 
<Text>{instructions[2]}</Text>
:<Text>{instructions[1]}</Text>
}

and if you need simple if condition then, 如果您需要简单的if条件,

{ condition == true && <Text>your text here</Text>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM