在组件之间共享数据

时间:2019-10-04 08:58:33

标签: reactjs

我有一个扩展形式的组件,例如:

import React from "react";
import Form from "./common/form";
import AppFeature from "./common/appFeature";
class AddFeature extends Form {
render() {
<AppFeature role={"newRole"} />}
}

当我尝试通过appfeature获取this.props.role组件中的角色值时。我会得到undefined

任何解决方法?

1 个答案:

答案 0 :(得分:0)

您的doSubmit功能不正确。

您需要将状态更新-将showFeatures更改为true,以呈现AddFeature组件。

您正在呼叫history.push到一个未定义的特定路由,并使用React Router来做到这一点。

使用以下代码,您将可以为AddFeature组件获取道具

  doSubmit = async ex => {
    try {
      this.setState({
        showFeatures: true
      })
    } catch (ex) {
      console.log(ex.response);
    }
  };