【问题标题】:React Native : Dynamically Created TextBox value is not getting updatedReact Native:动态创建的文本框值未更新
【发布时间】:2020-09-02 22:55:57
【问题描述】:

我对 React Native 并不陌生。我有一个场景,我需要动态创建 TextInput 并从数组中绑定值。数组更新后,TextInput 的值不会更新。下面是我的代码。

 constructor(props) {
    super(props);
    this.state = {
    textInputValues: [],
    textInput: [],
    samplearray://gets an array from the JSON
}

componentDidMount() {
    this.setTextInputValue();
    this.prepareTextBox();
}

setTextInputValue() {
  let textInputValues = this.state.textInputValues;
  this.state.samplearray.map(() => {
     textInputValues.push("") //default value
     this.setState({ textInputValues })
  })
}

prepareTextBox() {
let textInput = this.state.textInput;
this.state.samplearray.map((value, index) => {
    textInput.push(<TextInput style={styles.textBox} value={this.state.textInputValues[index]}  key={index} />);
})
this.setState({ textInput })}

在 render 方法中渲染 TextBox 的代码。

  {  this.state.textInput.map((value, index) => {
                        return value
  })}

我有一个 this.state.textInputValues 数组值被更改的按钮。但是这种变化并没有反映在 TextInput 中。自2天以来一直坚持这一点。任何帮助表示赞赏,在此先感谢。

【问题讨论】:

    标签: reactjs react-native mobile


    【解决方案1】:

    这是您的代码块的外观(请阅读 cmets 进行解释):

     componentDidMount() {
          this.setTextInputValue();
          // call the below function from `setTextInputValue` as you have dependency on that
          // this.prepareTextBox();
      }
    
      setTextInputValue() {
        let textInputValues = [...this.state.textInputValues];
        this.state.samplearray.map((value) => {
           textInputValues = [ ...textInputValues , value] //default value
    
           // this is how you should call `prepareTextBox`
           // in setState callback as it will confirm that state is updated
           this.setState({ textInputValues },() => {
              this.prepareTextBox();
           })
        })
      }
    
      prepareTextBox() {
        let textInput = [...this.state.textInput];
        this.state.samplearray.map((value, index) => {
            textInput.push(<input value={this.state.textInputValues[index]}  key={index} />);
        })
        this.setState({ textInput })
      }
    

    您可以运行下面的 sn-p 并检查,希望能消除您的疑虑:

    const { useState , useEffect } = React;
    
    class App extends React.Component {
      
      constructor(props) {
          super(props);
          this.state = {
          textInputValues: [],
          textInput: [],
          samplearray:["Vivek","Darshita"]//gets an array from the JSON
          }
      }
    
      componentDidMount() {
          this.setTextInputValue();
      }
    
      setTextInputValue() {
        let textInputValues = [...this.state.textInputValues];
        this.state.samplearray.map((value) => {
           textInputValues = [ ...textInputValues , value] //default value
           this.setState({ textInputValues },() => {
              this.prepareTextBox();
           })
        })
      }
    
      prepareTextBox() {
        let textInput = [...this.state.textInput];
        this.state.samplearray.map((value, index) => {
            textInput.push(<input value={this.state.textInputValues[index]}  key={index} />);
        })
        this.setState({ textInput })
      }
    
      changeValues = () => {
        this.setState({
          textInput : [],
          textInputValues : ["New - Vivek" , "New - Darshita"]
        },() => {
          this.prepareTextBox();
        });
      }
      
      render() {
        return (
          <div>
            { this.state.textInput }
            <button onClick={this.changeValues}>Change Value</button>
          </div>
        );
      }
      
    }
    
    ReactDOM.render(<App />, document.getElementById('react-root'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
    <div id="react-root"></div>

    【讨论】:

    • 感谢您的回答。能否在 sn-p 中添加按钮单击事件,这将更新 textInputValues 数组的值,从而更新输入框?我尝试调用一个名为 TestFunc() 的示例事件,它更新数组 textInputValues 的值,但文本输入的值没有改变。
    • 非常感谢您抽出宝贵的时间。我做的主要错误是,在按钮单击事件中,我没有设置调用 prepareTextBox 方法,也没有将 textInput 数组初始化为空。
    • @Vasanth,很高兴知道,它有帮助,也请接受答案。
    • 我在上述解决方案中遇到了一个问题。由于我们在单击按钮时刷新 textInput 数组,如果有很多文本框,那么刷新它会在 UI 中引入延迟。这是不可接受的。而是刷新整个 Textbox/Input 集,我们也可以刷新它指向的状态变量吗?
    • @Vasanth,是的,您必须找到并匹配并更新数组
    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2021-05-12
    • 2021-11-23
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多