Passing react text field input values as parameters to a method

前端 未结 2 1665
借酒劲吻你
借酒劲吻你 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:22

    How can I achieve this without storing the values in states?

    I think in this case better use states

    class App extends React.Component {
      constructor() {
        super();
        this.state = {
          topicBox: null,
          payloadBox: null
        };
        
        this.publish = this.publish.bind(this);
        this.handleChange = this.handleChange.bind(this);
      }
      
      handleChange({ target }) {
        this.setState({
          [target.name]: target.value
        });
      }
    
      publish() {
        console.log( this.state.topicBox, this.state.payloadBox );
      }
      
      render() {
        return 
    } } ReactDOM.render(, document.getElementById('container'));
    
    
    

提交回复
热议问题