简体   繁体   English

尝试动态包含用于SSR包的组件时,“函数作为React子无效”

[英]'Functions are not valid as a React child' when trying to dynamically include Components for SSR bundle

From my express route I'm trying to pass a component to use in a render function, that handles SSR. 从我的快速路线中,我试图传递要在处理SSR的渲染函数中使用的组件。

Express Route: 快速路线:

import SettingsConnected from '../../../client/components/settings/settings-connected';

function accountTab(req, res, next) {
  sendToRenderApp(req, res, { profileInfo }, url, SettingsConnected);
}

Render helper: 渲染助手:

export const sendToRenderApp = (req, res, storeObj = {}, urlPath, componentFunc) => {
  const store = configureStore(storeObj);
  const dynamicComponent = componentFunc;

  const component = (
    <Provider store={store}>
      <React.Fragment>
        <dynamicComponent />
      </React.Fragment>
    </Provider>
  );

  const sheet = new ServerStyleSheet();
  const app = renderToString(sheet.collectStyles(component));

Error: 错误:

" Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it. " 警告:函数作为React子元素无效。如果您返回Component而不是从render中返回,则可能会发生这种情况。或者您可能打算调用此函数而不是返回它。

Things I've already had a look at include this answer below, but I'm not sure how to wrap such a function inside (what I presume) the Provider component? 我已经看过的东西包括下面的答案,但是我不确定如何在Provider组件内部包装这样的函数(我认为是这样)?

Functions are not valid as a React child. 函数作为React子代无效。 This may happen if you return a Component instead of from render 如果您返回Component而不是从render返回,则可能会发生这种情况

ANSWERING OWN QUESTION: Turned out to be a problem with the client's hydrate. 回答自己的问题:原来是客户的水合物有问题。 This piece of code above was fine all along - :facepalm: 上面的这段代码一直很好-:facepalm:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图使用JS对象来填充Components却得到“函数作为React child无效”吗? - Trying to use JS object to populate Components but get “Functions not valid as React child”? 函数作为 React 子级无效。 - React 中的功能组件 - Functions are not valid as a React child. - Functional Components in React 如何解决“函数作为 React 子级无效”。 当我尝试根据函数的输出渲染组件时出错? - How to solve "Functions are not valid as a React child." error when I try to render a components based on the output of a function? 尝试映射时,对象作为 React 子对象无效 - Objects are not valid as a React child when trying to map 函数作为React子元素无效 - Functions are not valid as a React child 尝试从模块对象使用 HOC 时,“函数作为 React 子项无效” - "Functions are not valid as a React child" when trying to use HOC from a module object React-创建JSX组件数组时,对象作为React子对象无效 - React - Objects are not valid as a React child when creating array of JSX components 当子道具动态变化时,对象作为 React 子对象无效 - Objects are not valid as a React child when child props change dynamically React-函数作为React子代无效 - React - Functions are not valid as React child 错误:函数作为React子代无效 - Error: Functions are not valid as a React child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM