React: This may happen if you return a Component instead of <Component /> from render

huangapple go评论57阅读模式
英文:

React: This may happen if you return a Component instead of <Component /> from render

问题

函数作为 React 子元素是无效的。这可能是因为你从 render 方法中返回了一个组件,或者可能你本来想调用这个函数而不是返回它。

匹配到路径为 "/ "的叶子路由没有元素。这意味着默认情况下将使用空值渲染,导致页面显示为 "empty"。

在 pages 文件夹中的 change-account 文件夹下的 index.ts 代码如下图所示。我想基于 URL 中的参数在这里打开不同的页面:
React: This may happen if you return a Component instead of <Component /> from render

但是当我输入 URL 时,会显示下面的错误。

以下是 DeleteAccountPage 的代码:
React: This may happen if you return a Component instead of <Component /> from render

下面的图片是 createDeleteAccountPage 的代码:
React: This may happen if you return a Component instead of <Component /> from render

如何在 React 中解决这个错误?

英文:

I Am Getting This Error 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

And

Matched leaf route at location "/" does not have an element. This means it will render an with a null value by default resulting in an "empty" page.

Below picture is the index.ts code in the change-accouont folder in the pages folder. I want to open different pages here based on the parameters in the URL:
React: This may happen if you return a Component instead of <Component /> from render

But when i enter the url, will show the below error.

Below is the DeleteAccountPage code:
React: This may happen if you return a Component instead of <Component /> from render

Below picture is the createDeleteAccountPage code:
React: This may happen if you return a Component instead of <Component /> from render

How to fix this error in react?

答案1

得分: 2

if DeleteAccountPage and ChangeAccountPage are Component you need change your code to :

export default () => {
    if (retrieverUrlParamValue(changeTypeQueryParamNode)) {
        return <DeleteAccountPage />;
    } else {
        return <ChangeAccountPage />;
    }
}

for return a component like DeleteAccountPage

return <DeleteAccountPage />;
英文:

if DeleteAccountPage and ChangeAccountPage are Component you need change your code to :

export default ()=&gt;{
    if(retrieverUrlParamValue(changeTypeQueryParamNode)){
        return &lt;DeleteAccountPage /&gt;;
    }else{
        return &lt;ChangeAccountPage /&gt;;
    }
}

for return a component like DeleteAccountPage

return &lt;DeleteAccountPage /&gt;

答案2

得分: 0

您在代码块中没有返回任何内容。您可以这样做:

export default () => {
if(retrieverUrlParamValue(changeTypeQueryParamNode)){
return DeleteAccountPage;
}else{
return ChangeAccountPage;
}
}


但是,您需要小心如何使用这段代码。根据您分享的内容,您可能需要类似以下的内容:

import AccountPage from '上述文件';

// 在您的渲染函数中
const conditionalComponent = AccountPage();
const FinalComponent = conditionalComponent(React.Fragment);
return ;

这可以说是一个不好的做法。相反,您应该使用组件组合:

const DeleteAccountPage: FC<{ PageProvider: FC }> = ({ PageProvider }) => {
return (
...
);
}

// ...

export default (props: { PageProvider: FC }) => {
if(retrieverUrlParamValue(changeTypeQueryParamNode)){
return <DeleteAccountPage {...props} />;
}else{
return <ChangeAccountPage {...props} />;
}
}


这种方式使用组件时,您只需:

import AccountPage from '上述 index.ts 文件';

// ...
return ;

英文:

You are not returning anything in your code blocks. You can do something like:

export default ()=&gt;{
    if(retrieverUrlParamValue(changeTypeQueryParamNode)){
        return DeleteAccountPage;
    }else{
        return ChangeAccountPage;
    }
}

However you need to be careful how you use this code. Based on what you shared you'd need something like:

import AccountPage from &#39;the above file&#39;;

// Then in your render function
const conditionalComponent = AccountPage();
const FinalComponent = conditionalComponent(React.Fragment);
return &lt;FinalComponent /&gt;;

This is arguably bad practice. Instead you should use component composition:

const DeleteAccountPage : FC&lt;{ PageProvider: FC }&gt; = ({ PageProvider }) =&gt; {
   return ( 
      &lt;PageProvider&gt;...&lt;/PageProvider&gt;
   );
}

// ...

export default (props: { PageProvider: FC })=&gt;{
    if(retrieverUrlParamValue(changeTypeQueryParamNode)){
        return &lt;DeleteAccountPage {...props} /&gt;;
    }else{
        return &lt;ChangeAccountPage {...props} /&gt;;
    }
}

This way to use this component you just need:

import AccountPage from &#39;the above index.ts file&#39;;

// ...
return &lt;AccountPage PageProvider={React.Fragment} /&gt;

huangapple
  • 本文由 发表于 2023年2月16日 15:34:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定