网络网站开发培训,包装技术支持 东莞网站建设,企业网盘哪个最好用,建筑施工建设网站本章内容  目录 一、响应式设计思想二、React 中的事件绑定 继上一节我们简单实现一个 TodoList来更加了解编写组件的一些细节。本节继续这个案例功能的完成。 
一、响应式设计思想 
1、在原生的 JS中#xff0c;如果要实现点击”提交“按钮就将输入框的内容添加至页面列表中如果要实现点击”提交“按钮就将输入框的内容添加至页面列表中我们通常的步骤是 
首先给”提交“按钮绑定一个点击事件然后获取到 input输入框的 value值。接着通过 document.getElementById找到这个”列表“最外层的元素然后把 input框里的内容”挂载“到这个外层 DOM元素中 
2、然而在 React编码过程中由于 React是一个响应式的框架。它跟原生JS操作 DOM的方式思想完全不一样。React强调的是”自动感知数据的变化然后自动的生成DOM“。因此在 React代码编写过程中我们只需要关注”数据层“即可 
3、现在我们打开 TodoList.js文件使用 React的编程思想来编写功能。React编码只需要关注数据层按照功能的要求我们只需要两组”数据“一组”数据“存储input框里的值一组”数据“用来存储列表中的”每一项“ 
import React, { Component, Fragment } from react;class TodoList extends Component{constructor(props) {super(props) // ES6 的语法this.state  {inputValue: hello wolrd!, // input 输入框中的值list: [] // 列表里的数据}}render() {return (Fragmentdiv{/* 在 JSX 语法中标签里如果有 JS表达式需要使用 {} 括起来。注释也一样 */}{/* input 元素的 value 绑定在 state 的 inputValue 中因此输入框中的内容由 inputValue 的内容决定*/}input value{this.state.inputValue} /button 提交 /button/divulliReact 入门-01/liliReact 入门-02/liliReact 入门-03/li/ul/Fragment)}
}export default TodoList4、不停的修改 inputValue的值页面输入框的内容也随之变化。这也就是说React可以感知数据的变化主动的把数据映射到页面中而不需要像原生 JS那么麻烦的去操作 DOM 
5、当在页面输入框中不停的输入内容我们会发现里面的内容完全不会变化。这是因为 input框中的内容是由”组件“的 state状态对应的 inputValue值决定的。在代码里我们已经写死了所以不管我们怎么操作input框中的内容页面都毫无变化。那怎么解决这问题呢这就需要给 input绑定事件了 
二、React 中的事件绑定 
1、修改 TodoList.js中的代码给 input元素绑定事件 
import React, { Component, Fragment } from react;class TodoList extends Component{constructor(props) {super(props) // ES6 的语法this.state  {inputValue: , // 1、input 输入框中的值. 设置为空字符以便后续自由输入list: [] }}render() {return (Fragmentdiv{/* 2、给 input 绑定一个 onChange 的监听事件当输入框有内容变化时执行 changeInputValue 方法 */}{/* 3、React 绑定事件不同于原生 JS使用”驼峰“形式 */}{/* 4、”JSX 语法“ 要求将 ”JS 表达式“ 写在 {} 中 */}{/* 5、使用 bind(this), 将 this 的指向指为 TodoList  */}input value{this.state.inputValue} onChange{this.changeInputValue.bind(this)} /button 提交 /button/divulliReact 入门-01/liliReact 入门-02/liliReact 入门-03/li/ul/Fragment)}// 6、定义一个 方法传入 event 事件其 target 指向 input 框对应的 DOM 节点event.target.value 对应输入框中的 value 值。changeInputValue(e) {console.log(e.target.value)}
}export default TodoList2、运行代码再次操作页面的输入框可以看到控制台输出对应的内容  
3、但是有个问题就是控制台是可以看到内容的变化但是页面输入框仍然没啥变化。这个时候就要使用 React 为每个组件提供的 setState()方法用来改变组件的 state状态里的数据值 
import React, { Component, Fragment } from react;class TodoList extends Component{constructor(props) {super(props) // ES6 的语法this.state  {inputValue: , // 1、input 输入框中的值. 设置为空字符以便后续自由输入list: [] }}render() {return (Fragmentdiv{/* 2、给 input 绑定一个 onChange 的监听事件当输入框有内容变化时执行 changeInputValue 方法 */}{/* 3、React 绑定事件不同于原生 JS使用”驼峰“形式 */}{/* 4、”JSX 语法“ 要求将 ”JS 表达式“ 写在 {} 中 */}{/* 5、使用 bind(this), 将 this 的指向指为 TodoList  */}input value{this.state.inputValue} onChange{this.changeInputValue.bind(this)} /button 提交 /button/divulliReact 入门-01/liliReact 入门-02/liliReact 入门-03/li/ul/Fragment)}// 6、定义一个 方法传入 event 事件其 target 指向 input 框对应的 DOM 节点event.target.value 对应输入框中的 value 值。changeInputValue(e) {console.log(e.target.value)this.setState({inputValue: e.target.value})}
}export default TodoList4、再次操作界面发现输入框里的内容可以变化了  
到此本章的内容就结束了 文章转载自: http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.kwhrq.cn.gov.cn.kwhrq.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.gprzp.cn.gov.cn.gprzp.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.trrpb.cn.gov.cn.trrpb.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.lyhrg.cn.gov.cn.lyhrg.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.syhwc.cn.gov.cn.syhwc.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.xgchm.cn.gov.cn.xgchm.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.xcfmh.cn.gov.cn.xcfmh.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.rdzgm.cn.gov.cn.rdzgm.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.rpgdd.cn.gov.cn.rpgdd.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.ndfwh.cn.gov.cn.ndfwh.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.mgbcf.cn.gov.cn.mgbcf.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn