如何正确访问TextInput值并将其分配给状态变量?

时间:2020-10-05 10:59:51

标签: javascript react-native

我不断收到此错误

 implementation "androidx.core:core:1.5.0-alpha04"

即使抛出此错误,该代码似乎仍然可以工作。例如,当我将setState与textInput组件一起使用时,就会发生这种情况-

Warning: Cannot update a component from inside the function body of a different component.

 return (
      <View style={styles.container}>
        <View style={styles.inputContainer}>
          <View style={styles.textInputContainer}>
            <Text>Title of project:</Text>
            <TextInput
              editable={true}
              placeholder="Type here"
              value={this.state.title}
              style={styles.textInput}
              onChangeText={this.handleTitle}
            ></TextInput>

2 个答案:

答案 0 :(得分:0)

如果使用箭头函数,请勿在构造函数中绑定函数。

Sheet2

或者您可以使用内联函数

class CreateProject extends React.Component {
  constructor(props) {
    super(props);
    this.state = { title: "", dueDate: "", visibility: false };
  
  }

  handleTitle = (text) => {
    this.setState({ title: text });
  };

答案 1 :(得分:0)

您的代码似乎可以正常工作,请参见snack

相关问题