栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将React文本字段输入值作为参数传递给方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将React文本字段输入值作为参数传递给方法

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

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

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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382971.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号