react 主要用于构建ui,很多人认为 react 是 mvc 中的 v(视图)。
react 起源于 facebook 的内部项目,用来架设 instagram 的网站,并于 2013 年 5 月开源。
react 拥有较高的性能,代码逻辑非常简单,越来越多的人已开始关注和使用它。
最常见的就是父子组件之间传递参数
父组件往子组件传值,直接用this.props就可以实现
父组件中,给需要传递数据的子组件添加一个自定义属性,在子组件中通过this.props就可以获取到父组件传递过去的数据
// 父组件 render() { return ( // 使用自定义属性传递需要传递的方法或者参数 <shopui toson={this.state}></shopui> ) } //子组件 //通过this.props.toson就可以获取到父组件传递过来的数据 、、如果还需要往孙组件传递那么在子组件通过自定义属性继续传递就行了 tograndson={this.props.toson}、、孙组件通过this.props.tograndson获取到数据
子组件给父组件传值的话,需要在父组件设置接收函数和state,同时将函数名通过props传递给子组件
也就是给子组件传入父组件的方法,在子组件进行调用
//孙子组件export default class grandson extends component{ render(){ return ( <div style={{border: "1px solid red",margin: "10px"}}>{this.props.name}: <select onchange={this.props.handleselect}> <option value="男">男</option> <option value="女">女</option> </select> </div> ) }}; //子组件export default class child extends component{ render(){ return ( <div style={{border: "1px solid green",margin: "10px"}}> {this.props.name}:<input onchange={this.props.handleval}/> <grandson name="性别" handleselect={this.props.handleselect}/> </div> ) }}; //父组件export default class parent extends component{ constructor(props){ super(props) this.state={ username: '', sex: '' } }, handleval(event){ this.setstate({username: event.target.value}); }, handleselect(value) { this.setstate({sex: event.target.value}); }, render(){ return ( <div style={{border: "1px solid #000",padding: "10px"}}> <div>用户姓名:{this.state.username}</div> <div>用户性别:{this.state.sex}</div> <child name="姓名" handleval={this.handleval} handleselect={this.handleselect}/> </div> ) }}
前一段时间有人问过我这样一个问题,constructor里面的super()是干嘛用的?
总结一下:
如果要在子类的constructor里使用this,必须调用父类constructor,否则就拿不到this
那么问题就来了,如何调用父类的constructor呢? 通过super()
如果要在constructor里使用父组件传递过来的参数,必须在调用父组件super时,传递参数给父组件的constructor
如果不在constructor里面使用this,或者参数,就不需要super ; 因为react以及帮你做了this,props的绑定
路由传参
安装 npm install react-router-dom --save-dev
定义路由(一般会放在外面)
<hashrouter> <switch> <route exact path="/" component={home}/> <route exact path="/detail" component={detail}/> </switch> </hashrouter>
当页面跳转时
<li onclick={el => this.props.history.push({ pathname:'/detail', state:{id:3}})}></li>
接收 通过this.props.history.location可以获取到传递过来的数据
路由传参可能会有这个问题,就是只有在路由定义时挂载的组件中才会有props里面的location history match
路由上挂载的那个组件一般都是container.js,一般我们会往下分出ui.js组件,在这里面进行点击跳转,ui组件props里没有location history match
需要用到高阶组件withrouter
状态提升
将多个组件需要共享的状态提升到离他们最近的那个公共父组件上,然后父组件通过props分发给子组件
context
当某个组件在自己的context中保存了某个状态,那个该组件下的所有子孙组件都可以访问到这个状态,不需要中间组件的传递,而这个组件的父组件是没办法访问的
class index extends component { static childcontexttypes = { themecolor: proptypes.string } constructor () { super() this.state = { themecolor: 'red' } } getchildcontext () { return { themecolor: this.state.themecolor } } render () { return ( <div> <header /> <main /> </div> ) }}通过getchildcontext()将属性传递给所有的子孙组件提供 context 的组件必须提供 childcontexttypes 作为 context 的声明和验证。
class title extends component { static contexttypes = { themecolor: proptypes.string } render () { return ( <h1 style={{ color: this.context.themecolor }}>标题</h1> ) }}子组件要获取 context 里面的内容的话,就必须写 contexttypes 来声明和验证你需要获取的状态的类型,它也是必写的,如果你不写就无法获取 context 里面的状态。title 想获取 themecolor,它是一个字符串,我们就在 contexttypes 里面进行声明。
引入redux
redux为react提供可预测化的状态管理机制
redux将整个应用状态存储到store,store里保存着一个state状态树
组件可以派发(dispatch) 行为 (action) 给store , 而不是直接通知其它组件
其它组件可以通过订阅store中的状态state来刷新自己的视图
相关推荐:react教程
以上就是react有哪几种传递参数的方式的详细内容。
