【问题标题】:React HOC can't pass props to enhanced componentReact HOC 无法将道具传递给增强组件
【发布时间】:2019-08-12 14:39:19
【问题描述】:

我有这段代码:

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 &lt;Enhanced api={api}/&gt;;这一行会导致这些错误:

1.

警告:React.createElement:类型无效——需要一个字符串 (对于内置组件)或类/函数(对于复合 组件)但得到:.你有没有不小心 导出 JSX 文字而不是组件?

2.

未捕获的不变违规:元素类型无效:预期为 字符串(用于内置组件)或类/函数(用于复合 组件)但得到:对象。

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>
        )
    }
}

【问题讨论】:

    标签: javascript reactjs typescript react-context


    【解决方案1】:

    您没有正确编写 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>
            )
        }
    }
    

    【讨论】:

    • 感谢您的回复。刚刚尝试过,我收到此错误:“警告:函数作为 React 子级无效。如果您从渲染返回一个组件而不是 ,则可能会发生这种情况。或者您可能打算调用此函数而不是返回它。”
    • 谢谢!它工作得很好。但是为什么这是错误的:withApi()?
    • 因为您在 withApi 功能中将 JSX 作为增强型,并且您仍在尝试将其呈现为组件
    猜你喜欢
    • 2020-12-21
    • 2018-08-22
    • 2022-01-10
    • 2020-08-09
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多