【问题标题】:Passing react text field input values as parameters to a method将反应文本字段输入值作为参数传递给方法
【发布时间】:2017-02-05 00:15:02
【问题描述】:

我有以下输入字段,我需要获取输入的输入并将其传递给下面显示的按钮的 onClick 事件。

<input type="text" style={textFieldStyle} name="topicBox" placeholder="Enter topic here..."/>
<input type="text" style = {textFieldStyle} name="payloadBox" placeholder="Enter payload here..."/>
<button value="Send" style={ buttonStyle } onClick={this.publish.bind(this,<value of input field 1>,<value of input field2>)}>Publish</button><span/>

我有一个名为 publish 的方法,它接受两个字符串参数。代替这些字符串,我需要传递在输入字段中输入的值。如果不将值存储在状态中,如何实现这一点?我不想将输入字段值存储在状态变量中。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript button reactjs input typescript


    【解决方案1】:

    如何在不将值存储在状态中的情况下实现此目的?

    我认为在这种情况下更好地使用状态

    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 <div>
          <input 
            type="text" 
            name="topicBox" 
            placeholder="Enter topic here..." 
            value={ this.state.topicBox }
            onChange={ this.handleChange } 
          />
          
          <input 
            type="text" 
            name="payloadBox" 
            placeholder="Enter payload here..."
            value={ this.state.payloadBox } 
            onChange={ this.handleChange } 
          />
          
          <button value="Send" onClick={ this.publish }>Publish</button>
        </div>
      }
    }
    
    ReactDOM.render(<App />, document.getElementById('container'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <div id="container"></div>

    【讨论】:

    【解决方案2】:

    您可以为每个文本字段添加 ref,并从中读取值:

    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 <div>
          <input 
            ref="topic"
            type="text"
            name="topicBox"
            placeholder="Enter topic here..."/>
    
          <input 
            ref="payload"
            type="text"
            name="payloadBox"
            placeholder="Enter payload here..."/>
    
          <button 
            value="Send"
            onClick={this.publish}> 
            Publish
          </button>
        </div>
      }
    }
    
    ReactDOM.render(<App />, document.getElementById('container'));
    

    工作小提琴https://jsfiddle.net/hjx3ug8a/15/

    感谢 Alexander T 的加入!

    【讨论】:

    • 使用 refs 时出现以下错误。 ./src/components/Sender.tsx (126,102) 中的错误:错误 TS2339:类型 '{ [key: string] 上不存在属性“主题”:组件 |元素; }'。
    • 应该可以了,添加整个脚本看看哪里有问题。
    • @Muhammad Aref 将 .bind(this) 传递给 onClick 不是好的做法,您应该 .bind 包含构造函数 - github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/…
    • @Muhammad Aref 为什么在函数签名中使用变量 publish(topicBox, payloadBox) { ?
    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 2020-07-07
    • 2019-06-01
    相关资源
    最近更新 更多