简体   繁体   English

通过 react.js 中的 props 传递用户输入值

[英]Pass user input value through props in react.js

I have 2 component, how do I pass user entered value through onChange to parent component?我有 2 个组件,如何通过 onChange 将用户输入的值传递给父组件? I'm able to pass the 'trigger' upon onChange, but how to pass the value along?我可以在 onChange 上传递“触发器”,但是如何传递值呢?

https://jsfiddle.net/gboaxm30 https://jsfiddle.net/gboaxm30

var InputComp = React.createClass({
  render: function() {
    return (
    <div>
     <input type="text" onChange={this.props.newVal} />
     </div>
    );
  }
});

var App = React.createClass({
    getInitialState(){
  return {
     inputVal: 0
  }
  },
  inputChangedHandler(props) {
    //set user changed value to inputVal
    console.log(props)
  },
  render() {
    return(
    <div>
        <InputComp newVal={this.inputChangedHandler}/>
      <h4>{this.state.inputVal}</h4>
      </div>
    )
  }
})

ReactDOM.render(
  <App />,
  document.getElementById('container')
);

Call a function on the onChange event of the child component and then access the value of input like e.target.value and then pass it to the parent component like this.props.newVal(e.target.value);在子组件的onChange事件上调用一个函数,然后像e.target.value这样访问 input 的值,然后像this.props.newVal(e.target.value);一样将其传递给父组件this.props.newVal(e.target.value);

var InputComp = React.createClass({
  handleChange(e) {
    this.props.newVal(e.target.value);
  },
  render: function() {
    return (
    <div>
     <input type="text" onChange={this.handleChange} />
     </div>
    );
  }
});

var App = React.createClass({
    getInitialState(){
  return {
     inputVal: 0
  }
  },
  inputChangedHandler(val) {
    console.log(val);
    this.setState({inputVal: val});
  },
  render() {
    return(
    <div>
        <InputComp newVal={this.inputChangedHandler}/>
      <h4>{this.state.inputVal}</h4>
      </div>
    )
  }
})

ReactDOM.render(
  <App />,
  document.getElementById('container')
);

JSFIDDLE JSFIDDLE

I've made a demo for you here: http://codepen.io/PiotrBerebecki/pen/pEAQzV我在这里为你做了一个演示: http : //codepen.io/PiotrBerebecki/pen/pEAQzV

The idea is to use the so-called controlled input as defined in the React docs: https://facebook.github.io/react/docs/forms.html#controlled-components这个想法是使用 React 文档中定义的所谓受控输入: https : //facebook.github.io/react/docs/forms.html#controlled-components

 var InputComp = React.createClass({
  getInitialState() {
    return {
      userInput: ''
    };
  },

  onChange(event) {
    this.setState({
      userInput: event.target.value
    });
    this.props.newVal(event.target.value);
  },

  render() {
    return (
      <div>
         InputComp
         <input type="text" 
                value={this.state.userInput}
                onChange={this.onChange} />
      </div>
    );
  }
});




var App = React.createClass({
  getInitialState() {
    return {
     inputVal: ''
    };
  },

  inputChangedHandler(valueFromChild) {
    console.log('valuefromChild:', valueFromChild);
    this.setState({
      inputVal: valueFromChild
    });
  },

  render() {
    return (
      <div>
        <InputComp newVal={this.inputChangedHandler}/>
        <h4>{this.state.inputVal}</h4>
      </div>
    );
  }
})

ReactDOM.render(
  <App />,
  document.getElementById('container')
);

You should take the value from the event你应该从事件中获取价值

  inputChangedHandler(e) {
      //set user changed value to inputVal
      console.log(e.target.value)
  },

I thinktThis would be helpful for you.我想这对你有帮助。

let InputComp = React.createClass({
  getInitialState() {
    return {
      textVal: "",
    };
  },
  handleChange(e) {
    this.setState({ textVal: e.target.value });
    this.props.newVal(this.state.textVal);
  },
  render: function () {
    return (
      <div>
        <input
          type="text"
          onChange={this.handleChange}
          value={this.state.textVal}
        />
      </div>
    );
  },
});

var App = React.createClass({
  getInitialState() {
    return {
      inputVal: 0,
    };
  },
  inputChangedHandler(val) {
    this.setState({ inputVal: val });
  },
  render() {
    return (
      <div>
        <InputComp newVal={this.inputChangedHandler} />
        <h4>{this.state.inputVal}</h4>
      </div>
    );
  },
});

ReactDOM.render(<App />, document.getElementById("container"));

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

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