我正在使用connect
React Redux 函数将我的商店连接到 React 组件。连接后我想渲染组件。这有效:
class Init extends React.Component {
render() {
return (
<View /> // Is really more elaborate
)
}
}
将其连接到商店:
const InitPage = connect(
state => ({
}),
dispatch => ({
})
)(Init)
现在渲染它:
class App extends React.Component {
render() {
const content = <InitPage />
return (
{content}
)
}
}
伟大的。这一切都有效。然而...
当我将connect
内部放入一个函数并返回连接时,如下所示:
const getInitPage = () => {
const InitPage = connect(
state => ({
}),
dispatch => ({
})
)(Init)
return InitPage
}
如果我现在尝试渲染组件:
class App extends React.Component {
render() {
const content = getInitPage()
return (
{content}
)
}
}
我收到一个错误:
Invariant Violation: React.Children.only expected to receive a single React element child.
从函数返回 Redux“已连接”组件的正确方法是什么?
如果您从函数返回组件,则需要像这样渲染它
class App extends React.Component {
render() {
const Content = getInitPage()
return (
<Content/>
)
}
}
还要确保 componentName 以大写字符开头。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句