【问题标题】:Error: Object are not valid as a react child?错误:对象作为反应孩子无效?
【发布时间】:2019-07-03 18:23:00
【问题描述】:
export default class App extends Component<Props> {
constructor() {
    super();
    this.state = {
        value: "Edit me!"
    };
    this.handleChangeText = this.handleChangeText.bind(this);


}

handleChangeText(newText) {
    this.setState({
        value: newText
    });
}


render() {
    return (
        <View style={styles.container}>
            <TextInput style={styles.editText}
                       onChange={this.handleChangeText}/>
            <Text>hello {this.state.value}</Text>
        </View>
    );
}

我是原生反应的新手。我在 hello {this.state.value} 行上遇到错误。我该如何解决这个问题。请帮帮我

【问题讨论】:

标签: react-native


【解决方案1】:

你可以试试这个吗?

export default class App extends Component<Props> {
  constructor(Props) {
      super(props);
      this.state = {
        value: "Edit me!"
      };
  }
  handleChangeText = (newText) => {
      this.setState({
          value: newText
      });
  }
  render() {
      return (
          <View style={styles.container}>
              <TextInput
                style={styles.editText}
                onChangeText={this.handleChangeText}
                />
              <Text>hello {this.state.value}</Text>
          </View>
      );
  }
}

【讨论】:

  • 解释应该是你需要使用onChangeText来接收作为类型的文本。问题中的代码错误地使用了接收事件的onChange。由于这是一个对象而不是字符串,因此您将收到该错误,如果您想在链接的重复问题中进行解释,则该错误。您可以从事件对象中获取文本值,但 onChangeText 不需要这样做。
猜你喜欢
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多