Passing react text field input values as parameters to a method

前端 未结 2 1671
借酒劲吻你
借酒劲吻你 2021-02-19 08:41

I have the below input fields of which I need to get the entered inputs and pass it to the onClick event of the button shown below.



        
2条回答
  •  天涯浪人
    2021-02-19 09:39

    You can add ref for each text field, and read the value from it like:

    class App extends React.Component {
      constructor() {
        super();
        this.publish = this.publish.bind(this);
      }
    
      publish(topicBox, payloadBox) {
        alert(this.refs.topic.value);
        alert(this.refs.payload.value);
      }
    
      render() {
        return 
    } } ReactDOM.render(, document.getElementById('container'));

    Working fiddle https://jsfiddle.net/hjx3ug8a/15/

    Thanks for Alexander T for his addition!

提交回复
热议问题