安装create-react-apphtml
npm install create-react-app -g
建立项目前端
creact-react-app demos cd demos npm start
package.jsonnode
"scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" }
npm start
启动开发环境,npm run build
建立生产环境优化代码,npm test
用于测试单元,npm run eject
把潜藏在react-scripts中的一序列技术栈“弹射”到
应用的顶端,此命令不可逆且会改变和增长一些文件。react
- 用户看到的界面(UI),是一个 纯函数(render) 的执行结果,只接受数据(data)做为参数;
- 纯函数:没有任何反作用,输出彻底依赖于输入的函数;
- 对于react开发者,重要的是区分哪些属于data,哪些属于render,要更新界面,要作的就是更新data;
- react实践的也是"响应式编程"的思想。
属性使用算法
data-*
;JavaScript表达式使用npm
样式编程
注释json
数组数组
事件挂载浏览器
HTML直接使用onclick缺点:
JSX中的onClick事件(不存在以上问题)
function Demo(){ const name = 'jsx'; const arr = [ <h3 key = {0}>数组</h3> <p key = {1}>数组会自动展开,注意添加key属性</p> ]; const func = () => { let result = 'hello'; if (name){ result += name; } else { result += 'world'; } return result; }; return ( <div> <h2></h2> {/*注释*/} <p style = {{color: 'red',fontSize: '14px'}}>hello {name || 'world'}</p> <p className = {name ? 'classA' : 'classB'}> hello {name && 'world'} </p> <p> {func()} </p> {arr} </div> ) }
React的prop
给prop赋值
class Demo extends Component{ render(){ return( <div> <Child caption = "toProp" initValue = {0}/>//给子组件<Child />传入caption和initValue信息,子组件需定义相关prop接口 </div> ) } }
读取prop值
- 给
this.prop
赋值是React.Component
构造函数的工做之一;- 若是一个组件须要定义本身的构造函数,必定要在构造函数的第一行
super
调用父类也就是React.Component
的构造函数;- 若是没有在构造函数中调用
super(props)
,那么组件实例被构造以后,类实例的全部成员就没法经过this.props
访问到父组件传递过来的props
值。
class Child extends Component{ constructor(props){ super(props); this.state = { //获取外部传入的prop,并用于state初始化 count: props.initValue || 0, caption: props.caption } } }
propTypes检查
React.PropTypes.*
,需导入prop-types
npm install prop-type --save
导入import PropTypes from ('prop-types')
propTypes验证器
PropTypes.array
PropTypes.bool
PropTypes.func
PropTypes.number
PropTypes.object
PropTypes.string
PropTypes.node
PropTypes.element
PropTypes.instanceOf(Message)
PropTypes.oneOf(['News','Photos'])
PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Message)
])
PropTypes.arrayOf(PropTypes.number)
PropTypes.objectOf(PropTypes.number)
PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number
})
PropTypes.func.isRequired
eg: Child.propTypes = { initValue: PropTypes.number, caption: PropTypes.string }
state表明组件的内部状态,因为React组件不能修改传入的prop,因此须要使用state记录自身数据变化;
state初始化
constructor(props){ ... this.state = { count: props.initValue || 0 } }
注意:使用React.createClass方法建立出来的组件类,经过getInitialState方法获取初始值,但这种方法已被废弃。
读取和更新state
this.state
this.setState({})
注意:不要直接修改this.state的值,虽然可以改变组件的内部状态,但只是野蛮的修改了state,却不会驱动组件重新渲染,因此变化不会反应到界面
而,this.setState()
所作的事是先改变this.state
的值,而后驱动组件更新
prop和state对比
使用context能够实现跨级传递。
context使用步骤
eg:
父组件
class Parent extends React.Component{ getChildContext(){ return {color: "red"} } render(){ return( <div> <Child /> </div> ) } } Parents.childContextTypes = { color: PropTypes.string.isRequired }
(有状态)子组件:
class Child extends React.Component{ render(){ return( <div> <p style = {{color:{this.context.color}}}>有状态的组件能够经过this.context获取</p> <Grandchild /> </div> ) } } Child.contextTypes = { color: PropTypes.string.isRequired }
(无状态)孙子组件:
function Grandchild(context){ return( <p style = {{color: {context.color}}}>无状态组件能够直接在函数的参数中获取</p> ) } Grandchild.contextTypes = { color:PropTypes.string.isRequired }
不积跬步,何以行千里