You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varInput=React.createClass({getInitialState: function(){return{firstName: "Kuitos",lastName: "Lau"};},handleChange: function(event){this.setState({firstName: event.target.value});},componentWillMount: function(){console.log("dom will be insert",this.state.firstName);},componentDidMount: function(){console.log("dom had be insert",this.state.firstName);},componentWillUpdate: function(nextProps,nextState){console.log("dom will be update",nextProps,nextState);},componentDidUpdate: function(prevProps,prevState){console.log("dom had be update",prevProps,prevState);},render: function(){varstate=this.state;return(<div><p>{state.firstName}{state.lastName}</p><inputtype="text"value={state.firstName}author={state.firstName}onChange={this.handleChange}/></div>);}});React.render(<Input/>,document.getElementById("inputDataBind"));
打印的顺序依次是,dom will be update , dom had be update
当input输入时 dom will be update , dom had be update
前端已不止于前端-ReactJs初体验
原文写于 2015-04-15
要说2015年前端届最备受瞩目的技术是啥,当然非ReactJs莫属。作为一个只关注最前沿前端技术的系列,自然少不了关于它的介绍。
ReactJs简介
ReactJs核心特征
说了这么多理论性的东西,还是直接来上代码吧
ReactJs开发准备工作
首先你需要reactjs的开发环境。
脚本中引入react,由于我们需要使用jsx语法提高开发效率,所以还需要引入能讲jsx转化为javascript的库
不过这样JSXTransformer每次都会在app打开的时候做转换工作,并不适合生产环境,转换工作应该放在服务端进行,借助jsx工具
然后页面依赖改成这样
node插件会在你开发的时候自动将源码转成javascript文件到指定目录
第一个react程序
接下来我们介绍一下react的一些基础语法
React.render() 将模版转换成html并插入到指定节点 参见上文的hello world示例
React解析引擎的规则就是遇到<符号就以jsx语法解析,遇到{就以javascript语法解析。比如这样
通过查看转换后的代码,我们可以看到他摘下面具后长这样
如何创建组件
通过React.createClass可以创建一个关联了虚拟dom的组件对象,每次组件数据更新便会调用组件的 render 方法重新渲染dom。
组件对象的props属性
上面一个例子我们看到在组件render方法中我们可以通过this.props.xx的方式拿到组件上关联的属性。另外需要额外提到的是,this.props有一个特殊属性children,它指向组件的子节点集合,like this
页面渲染的结果就是一个 ol 列表中还有两个li,每个li中包含一个超链接。通过这里我们也可以看出,在jsx中{}是会getValue的
获取真实dom React.findDOMNode()
组件状态 this.state
用React的方式实现angular中双向绑定的效果
virtual dom状态变更回调
组件生命周期分为三个状态:
我们这样写
打印的顺序依次是,dom will be update , dom had be update
当input输入时 dom will be update , dom had be update
react的基本知识就介绍到这里,后续我们会继续介绍react在实战项目中的应用及react native在移动端的表现力。
The text was updated successfully, but these errors were encountered: