javascript - React HOC 无法将 Prop 传递给增强组件

标签 javascript reactjs typescript react-context

我有这段代码:

const defaultValue = new Api()

const ApiContext = React.createContext(defaultValue);
const ApiProvider = ApiContext.Provider;
const ApiConsumer = ApiContext.Consumer;


const withApi = (Enhanced: any) => {

    return (        
        <ApiConsumer>
            {api => {
                return <Enhanced api={api}/>;
            }}
        </ApiConsumer>
    )
}

export default ApiContext;
export {ApiContext, ApiProvider, ApiConsumer, withApi};

在我的应用程序中,我有这样的东西:

const api = new ApiManager({...});

ReactDOM.hydrate(
    <Provider store={store}>
        <BrowserRouter>
            <ApiProvider value={api}>
                <Main />
            </ApiProvider>
        </BrowserRouter>
    </Provider>, document.querySelector('#app')
);

但是这一行return <Enhanced api={api}/>;导致这些错误:

1.

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: . Did you accidentally export a JSX literal instead of a component?

2.

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

3.

Uncaught (in promise) Invariant Violation: 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 `Context.Consumer`.

我在这里做错了什么? 如何将 api Prop 传递给增强组件?

[编辑]

这就是我调用组件的方式:

App.tsx

class App extends React.Component {
    render() {
        return (
            <React.Fragment>                
                    <Switch>
                        {routes.map(({ path, exact, component: C }) => {

                            return <Route
                                key={path}
                                path={path}
                                exact={exact}
                                render={(props) => {

                                    return withApi(<C {...props} />);

                                }} />
                        })}
                    </Switch>                
            </React.Fragment>
        )
    }
}

最佳答案

您没有正确编写 withApi HOC。它应该返回一个功能组件而不是 JSX

const withApi = (Enhanced: any) => {
  return (props) => {
    return (        
        <ApiConsumer>
            {api => {
                return <Enhanced {...props} api={api}/>;
            }}
        </ApiConsumer>
    )
  }
}

像这样使用它

class App extends React.Component {
    render() {
        return (
            <React.Fragment>                
                    <Switch>
                        {routes.map(({ path, exact, component: C }) => {
                            const Comp = withApi(C);
                            return <Route
                                key={path}
                                path={path}
                                exact={exact}
                                render={(props) => {

                                    return <Comp {...props}/>

                                }} />
                        })}
                    </Switch>                
            </React.Fragment>
        )
    }
}

关于javascript - React HOC 无法将 Prop 传递给增强组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55285559/

相关文章:

javascript - 错误 : <Route> elements are for router configuration only and should not be rendered in react-router v4

typescript - 无法使用 pixi.js 加载图像

angularjs - $onInit 没有被 typescript angularjs Controller 调用

javascript - Jasmine 模拟失败,错误为 `apply`,但正常函数调用正常

javascript - Jquery 和 WAI ARIA

javascript - 如何在 React js 0.13.3 中访问嵌套对象

reactjs - 使用 Flexbox 和 Tachyons 的响应式布局

javascript - typescript 对象奇怪的类型语法

javascript - Angular ng-repeat错误显示

javascript - AngularJS:如何从服务添加和编译指令到 dom