使用ReactJS新上下文API时,React-router-V4呈现错误

时间:2018-04-25 22:24:33

标签: javascript reactjs react-router-v4 react-context

我正在尝试将React Router V4与新的React的上下文API一起使用。这是我的代码:

class App extends Component {
    static propTypes = {
        router: PropTypes.func.isRequired,
        module: PropTypes.string.isRequired,
        sideMenuConfig: PropTypes.string
    };

    render() {

        let baseName = "/" + this.props.module;

        // This will get my dashboard component, loading context from database
        let navComponent = (
            <MyCustomAppContextProvider>
                <AppContext.Consumer>
                    {context => 
                        <Dashboard
                            context={context}
                            module={this.props.module}
                            router={router}
                            sideMenuConfig={this.props.sideMenuConfig}
                        />
                    }
                </AppContext.Consumer>
            </MyCustomAppContextProvider>
            );

        let routes = null;
        if (!isAuthenticated()) {
            routes = <Auth 
                        baseName={baseName}
                     />;
        } else {
            routes = (
                <Switch>
                    <Route exact path="/logout" component={Logout} />
                    <Route exact path="/auth" component={Logout} />
                    <Route exact path="/" component={navComponent} />
                    <Route
                        exact
                        path="/:screen"
                        component={navComponent}
                    />
                    <Route
                        exact
                        path="/:screen/:action"
                        component={navComponent}
                    />
                    <Route
                        exact
                        path="/:screen/:action/:id"
                        component={navComponent}
                    />

                    <Route component={PageNotFoundError} />
                </Switch>
            );
        }

        return (
            <BrowserRouter basename={baseName}>
                {routes}
            </BrowserRouter>
        );
    }
}

export default App;

router包含一个路由器功能,它将根据导航路径加载我的屏幕 module是模块名称 sideMenuConfig是我的侧边菜单的json配置。

尝试运行时,我从react路由器收到以下错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

Check the render method of `Route`.

我希望在Dashboard中接收所有传递的道具以及匹配属性来运行路由器。

2 个答案:

答案 0 :(得分:1)

问题是您没有将组件传递给Route

// THIS IS NOT A COMPONENT

let navComponent = (
  <MyCustomAppContextProvider>
    <AppContext.Consumer>
      {context => (
        <Dashboard
          context={context}
          module={this.props.module}
          router={router}
          sideMenuConfig={this.props.sideMenuConfig}
        />
      )}
    </AppContext.Consumer>
  </MyCustomAppContextProvider>
)

组件是扩展class的{​​{1}}或函数:

React.Component

更新到您的更新:

你对什么是组件感到困惑

// THIS IS A COMPONENT 

let NavComponent = props => (
  <MyCustomAppContextProvider>
    <AppContext.Consumer>
      {context => (
        <Dashboard
          context={context}
          module={this.props.module}
          router={router}
          sideMenuConfig={this.props.sideMenuConfig}
        />
      )}
    </AppContext.Consumer>
  </MyCustomAppContextProvider>
)

您需要将组件传递给Route的组件prop:

// THIS IS A COMPONENT
let Foo = props => <div />

// THIS IS NOT A COMPONENT
let Foo = <div />

答案 1 :(得分:0)

我也发生了同样的事情,正是那个上下文API导致了冲突。我还必须检查render方法中的每个内部组件,以确保它们中没有一个使用上下文api,并且内部没有this.context可用。然后,我删除了上下文API的内容,并编写了一个替代的react-redux状态,没有上下文api,没有问题