欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

初学react(四):组件属性传递(传事件)

程序员文章站 2022-03-11 16:09:43
...

React-属性传递(传事件)

a. 可以把函数当作属性传递过去,子组件内props接收

b.注意普通函数不能直接加括号,会直接执行

{/* 函数不能加括号,加括号就直接执行了,使用箭头函数可以实现参数的传递 */}
{/* <button onClick={() => this.switchNameHandLer('古天乐')}>更改状态值</button> */}
{/* 第二种方式  bind 传递 (建议) */}
<button onClick={this.switchNameHandLer.bind(this, '古天乐')}>更改状态值</button>
<Persion 
name={this.state.persions[0].name}
count={this.state.persions[0].count}/>
<Persion 
name={this.state.persions[1].name}
{/*传递函数*/}
myclick={this.switchNameHandLer.bind(this, '渣渣辉2')}
count={this.state.persions[1].count}/>
<Persion name={this.state.persions[2].name}
count={this.state.persions[2].count}/>
<Persion name={this.state.persions[3].name}
count={this.state.persions[3].count}>
<a href='http://www.baidu.com'>非常感谢大家的支持</a>
</Persion>
相关标签: reactjs