繁体   English   中英

反应道具 function 未定义 [重复]

[英]React prop function undefined [duplicate]

我有一个元素,它有一个 onClick 道具,然后是另一个调用这个道具的元素,并执行一些其他操作。

这是代码:

仪表板侧边栏:

class DashboardSidebar extends Component{
constructor(props){
    super(props);

    this.expandOption = this.expandOption.bind(this);
}

expandOption(id){
   ...
}

render(){
    return(
        <div className="DashboardSidebar">
            ...
            <DSidebarOption onClick={this.expandOption("dashboard")} id="dashboard" text="Dashboard" />
            ...
        </div>
    );
}
}

边栏选项:

class DSidebarOption extends Component{
constructor(props){
    super(props);

    this.handleClick = this.handleClick.bind(this);
}

handleClick(){
    ...
    this.props.onClick();
    ...
}


render(){
    return(
        <div className="DSidebarOptionContainer">
            ...
            <div onClick={this.handleClick} className="DSidebarOption">
                {this.props.text}
            </div>
            ...
        </div>
    )
}
}

我收到以下错误:

Uncaught TypeError: this.props.onClick is not a function

任何想法为什么会发生这种情况?

你应该像这样通过你的 onclick

onClick={() => {this.expandOption("dashboard")}}

否则它会在页面挂载时被调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM