如何将函数传递给子组件作为道具?

时间:2019-02-18 07:52:18

标签: reactjs react-router

我想将函数SELECT * FROM preview.entity_types 传递给子组件isLoggedIn作为道具。 即使由于HandleArticle问题而使用箭头功能,也会发生this

TypeError: this.isLoggedIn is not a function

1 个答案:

答案 0 :(得分:1)

根据documentation,箭头语法仍处于提议函数传递阶段。

isLoggedIn = () => {
    if (!this.props.isSignedIn) {
      history.push("/");
    } else {
      return <div>hello</div>;
    }
  };

构造函数中使用 bind 语法用于isLoggedIn函数

this.isLoggedIn = this.isLoggedIn .bind(this);

绑定直接在渲染功能

<HandleArticle isLoggedIn={this.isLoggedIn.bind(this)} />
相关问题