react在组件中触发子组件方法 以及触发兄弟组件
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 13px">触发子组件方法</span></strong></p><p><span style="font-family: "Microsoft YaHei"; font-size: 13px">第一种办法:</span></p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">class App extends React.Component{
render(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><React.Fragment>
<button className="btn" onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.clear.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>)}>清空</button>
<Input ref="input"></Input>
</React.Fragment>
<span style="color: rgba(0, 0, 0, 1)"> )
}
clear(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过refs拿到input组件对象,调用下面的方法</span>
<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.refs.input.clear()
}
}</span></pre>
</div>
<p> </p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">第二种办法:</span></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">我们知道在子组件中可以通过 this.props.methodName 调用父组件方法</span></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">因此我们可以通过给子组件props添加一个事件,在子组件的constructor中执行,将子组件的this作为参数传入,这样我们就可以在父组件中拿到子组件中的this</span></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">举例:有Input组件,内部有clear方法,我们需要在Input外层触发Input组件内部的clear去清空当前输入框</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei"; font-size: 13px"><span style="color: rgba(0, 0, 0, 1)">class App extends React.Component{
render(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><React.Fragment>
<button className="btn" onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.clear.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>)}>清空</button>
<Input inputEvent={<span style="color: rgba(0, 0, 255, 1)">this</span>.inputEvent.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>)}></Input>
</React.Fragment>
<span style="color: rgba(0, 0, 0, 1)"> )
}
inputEvent(input){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将子组件对象绑定到父组件的$child属性,这个属性名可以自己随便起</span>
<span style="color: rgba(0, 0, 255, 1)">this</span>.$child =<span style="color: rgba(0, 0, 0, 1)"> input
}
clear(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">点击按钮触发子组件clear方法</span>
<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.$child.clear()
}
}</span></span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">在Input组件中</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei"; font-size: 13px"><span style="color: rgba(0, 0, 0, 1)">class InputComponent extends React.Component{
constructor(props){
super(props)
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
inputVal: </span>''<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行父组件的方法,将this传过去</span>
<span style="color: rgba(0, 0, 255, 1)">this</span>.props.inputEvent(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">)
}
render(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><div className="input-container">
<input type="text" value={<span style="color: rgba(0, 0, 255, 1)">this</span>.state.inputVal} onChange={<span style="color: rgba(0, 0, 255, 1)">this</span>.inputChangeHandler.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>)} />
</div>
<span style="color: rgba(0, 0, 0, 1)"> )
}
inputChangeHandler(e){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
inputVal: e.target.value
})
}
clear(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
inputVal: </span>''<span style="color: rgba(0, 0, 0, 1)">
})
}
}
</span></span></pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 13px">触发兄弟组件的方法</span></strong></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">原理和上面类似,假设父组件A有子组件B和C,如果要在B中触发C的方法,需要将C的实例通过props传递给A,然后在B中调用props触发A的方法,间接触发C</span></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">实例:Input组件中点击按钮后需要在List组件中添加一条数据:</span></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">父组件:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei"; font-size: 13px"><span style="color: rgba(0, 0, 0, 1)">class App extends React.Component{
render(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><React.Fragment>
<button className="btn" onClick={() => <span style="color: rgba(0, 0, 255, 1)">this</span>.$child_input.clear()}>清空</button>
<Input inputEvent={input => <span style="color: rgba(0, 0, 255, 1)">this</span>.$child_input = input} addEvent={item => <span style="color: rgba(0, 0, 255, 1)">this</span>.$child_list.addRecord(item)}></Input>
<List listEvent={list => <span style="color: rgba(0, 0, 255, 1)">this</span>.$child_list = list}></List>
</React.Fragment>
<span style="color: rgba(0, 0, 0, 1)"> )
}
}</span></span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">Input组件:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei"; font-size: 13px"><span style="color: rgba(0, 0, 0, 1)">class InputComponent extends React.Component{
constructor(props){
super(props)
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
inputVal: </span>''<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.props.inputEvent(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">)
}
render(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><div className="input-container">
<input type="text" value={<span style="color: rgba(0, 0, 255, 1)">this</span>.state.inputVal} onChange={<span style="color: rgba(0, 0, 255, 1)">this</span>.inputChangeHandler.bind(<span style="color: rgba(0, 0, 255, 1)">this</span>)} />
<button className="btn" onClick={() => <span style="color: rgba(0, 0, 255, 1)">this</span>.props.addEvent(<span style="color: rgba(0, 0, 255, 1)">this</span>.state.inputVal) }>Submit</button>
</div>
<span style="color: rgba(0, 0, 0, 1)"> )
}
inputChangeHandler(e){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
inputVal: e.target.value
})
}
clear(){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
inputVal: </span>''<span style="color: rgba(0, 0, 0, 1)">
})
}
}</span></span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 13px">List组件:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei"; font-size: 13px"><span style="color: rgba(0, 0, 0, 1)">class MyList extends React.Component{
constructor(props){
super(props)
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {
data: [
</span>'Racing car sprays burning fuel into crowd.'<span style="color: rgba(0, 0, 0, 1)">,
</span>'Japanese princess to wed commoner.'<span style="color: rgba(0, 0, 0, 1)">,
</span>'Australian walks 100km after outback crash.'<span style="color: rgba(0, 0, 0, 1)">,
</span>'Man charged over missing wedding girl.'<span style="color: rgba(0, 0, 0, 1)">,
</span>'Los Angeles battles huge wildfires.'<span style="color: rgba(0, 0, 0, 1)">
]
}
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.props.listEvent(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">)
}
render(){
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><ul className="list-container"><span style="color: rgba(0, 0, 0, 1)">
{</span><span style="color: rgba(0, 0, 255, 1)">this</span>.state.data.map((item, index) =><span style="color: rgba(0, 0, 0, 1)"> (
</span><li key={index}>{item}</li>
<span style="color: rgba(0, 0, 0, 1)"> ))}
</span></ul>
<span style="color: rgba(0, 0, 0, 1)"> )
}
addRecord(item){
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({
data: [...</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.state.data, item]
})
}
}</span></span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/xiaoliwang/p/11237747.html
頁:
[1]