为了账号安全,请及时绑定邮箱和手机立即绑定

调用通过其 props 传递的函数

调用通过其 props 传递的函数

莫回无 2021-06-09 08:15:29
我目前是 react-bootstrap 的新手,有一个我无法解决的问题。我想通过将它作为道具传递给它的孩子来设置我的父母状态。当调用该函数时,它似乎被执行,但未设置状态.. 我不知道为什么会这样。class Parent extends React.Component{    constructor(props){        super(props);        this.state={            component : ""        }        this.returnDashboard = this.returnDashboard.bind(this);    }    returnDashboard=()=>{        console.log("i am executed")        this.setState({component : <Dashboard />})    }    render(){                   return (            <Child returnDashboard={this.returnDashboard}/>        );    }};class Child extends React.Component{    constructor(props){        super(props);    }    render(){         return(                <button onClick={()={this.props.returnSettings()}}>foo</button>            );    }}当按下按钮时,控制台会记录“我被执行了”和这个:ƒ () { [native code] }。有人可以帮我吗?
查看完整描述

2 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

你有一个错字

onClick={()={this.props.returnSettings()}}

应该

onClick={() => this.props.returnSettings()}

或者更简单

onClick={this.props.returnSettings}


查看完整回答
反对 回复 2021-06-11
?
LEATH

TA贡献1936条经验 获得超6个赞

你注意到子组件中有一个 returnSettings 道具,像这样点击事件

onClick = {()=>this.props.returnDashboard}


查看完整回答
反对 回复 2021-06-11
  • 2 回答
  • 0 关注
  • 178 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信