英文:
React: This may happen if you return a Component instead of <Component /> from render
问题
函数作为 React 子元素是无效的。这可能是因为你从 render
方法中返回了一个组件,或者可能你本来想调用这个函数而不是返回它。
匹配到路径为 "/ "的叶子路由没有元素。这意味着默认情况下将使用空值渲染,导致页面显示为 "empty"。
在 pages 文件夹中的 change-account 文件夹下的 index.ts 代码如下图所示。我想基于 URL 中的参数在这里打开不同的页面:
但是当我输入 URL 时,会显示下面的错误。
以下是 DeleteAccountPage 的代码:
下面的图片是 createDeleteAccountPage 的代码:
如何在 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:
But when i enter the url, will show the below error.
Below is the DeleteAccountPage code:
Below picture is the createDeleteAccountPage code:
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 ()=>{
if(retrieverUrlParamValue(changeTypeQueryParamNode)){
return <DeleteAccountPage />;
}else{
return <ChangeAccountPage />;
}
}
for return a component like DeleteAccountPage
return <DeleteAccountPage />
答案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 ()=>{
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 'the above file';
// Then in your render function
const conditionalComponent = AccountPage();
const FinalComponent = conditionalComponent(React.Fragment);
return <FinalComponent />;
This is arguably bad practice. Instead you should use component composition:
const DeleteAccountPage : FC<{ PageProvider: FC }> = ({ PageProvider }) => {
return (
<PageProvider>...</PageProvider>
);
}
// ...
export default (props: { PageProvider: FC })=>{
if(retrieverUrlParamValue(changeTypeQueryParamNode)){
return <DeleteAccountPage {...props} />;
}else{
return <ChangeAccountPage {...props} />;
}
}
This way to use this component you just need:
import AccountPage from 'the above index.ts file';
// ...
return <AccountPage PageProvider={React.Fragment} />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论