Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应设置输入';s值_Javascript_Reactjs - Fatal编程技术网

Javascript 反应设置输入';s值

Javascript 反应设置输入';s值,javascript,reactjs,Javascript,Reactjs,我在许多React教程中看到了有关管理输入值的内容。以下模式: 在将道具传递给输入组件的父级上,handleInputText设置anyValue的状态: <InputComponent textValue={this.state.anyValue} onInputtingText={this.handleInputText}/> 现在,我的发现是,我尝试在父母设置状态时记录它,它记录初始状态。以下是一些初步结论: 每当事件触发时,输入的值不是InputComponent的当前值。

我在许多React教程中看到了有关管理输入值的内容。以下模式:

在将道具传递给输入组件的父级上,handleInputText设置anyValue的状态:

<InputComponent textValue={this.state.anyValue} onInputtingText={this.handleInputText}/>
现在,我的发现是,我尝试在父母设置状态时记录它,它记录初始状态。以下是一些初步结论:

  • 每当事件触发时,输入的值不是InputComponent的当前值。它是在父对象上设置为该值的值

  • 第二次触发事件时,输入值和this.props.textValue都匹配

  • 我的问题是,你是如何处理这个问题的?还是必须在handleInput函数中检查此项


    提前感谢。

    您可以在
    这个.handleInputText中设置状态,并在
    输入组件中调用它

    var App = React.createClass({
      getInitialState() {
        return { anyValue: '' };
      },
    
      handleInputtingText(value) {
        this.setState({ anyValue: value });
      },
    
      render() {
        return <div>
          <p>{ this.state.anyValue }</p>
          <InputComponent 
            textValue={ this.state.anyValue } 
            onInputtingText={ this.handleInputtingText } 
          />
        </div>
      }
    });
    
    var InputComponent = React.createClass({
      handleInput(e) {
        this.props.onInputtingText(e.target.value);
      },
    
      render: function() {
        return <input 
          type="text" 
          value={this.props.textValue} 
          onChange={ this.handleInput } 
        />;
      }
    });
    
    var-App=React.createClass({
    getInitialState(){
    返回{anyValue:''};
    },
    HandleInputingText(值){
    this.setState({anyValue:value});
    },
    render(){
    返回
    {this.state.anyValue}

    } }); var InputComponent=React.createClass({ 手动输入(e){ this.props.onInputText(即target.value); }, render:function(){ 返回; } });

    是的,同样如此,我刚刚发现,每当我尝试在设置时记录状态值时,它都会返回初始值。为什么会这样?我下载了React调试器,状态设置为正确的值。谢谢
    handleInput(){
      this.setState(this.refs.inputRef.value)
    }
    
    var App = React.createClass({
      getInitialState() {
        return { anyValue: '' };
      },
    
      handleInputtingText(value) {
        this.setState({ anyValue: value });
      },
    
      render() {
        return <div>
          <p>{ this.state.anyValue }</p>
          <InputComponent 
            textValue={ this.state.anyValue } 
            onInputtingText={ this.handleInputtingText } 
          />
        </div>
      }
    });
    
    var InputComponent = React.createClass({
      handleInput(e) {
        this.props.onInputtingText(e.target.value);
      },
    
      render: function() {
        return <input 
          type="text" 
          value={this.props.textValue} 
          onChange={ this.handleInput } 
        />;
      }
    });