如何在渲染组件时处理错误

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

how to handle error while rendering component

问题

以下是您要翻译的代码部分:

Let's say I have a child component

function MainContent(prop) {
  return (
    <main>
      <p>{prop.message.message.substring(1, 5)}</p>
    </main>
  );
}

and rendering it in parent like

function App() {
  const data = [
    {
      message: 'first message',
    },
    {
      message: 'second message',
    },
    {
      //message: 'third message', working
      wrongkey: 'third message', // not working
    };
    return (
      <div>
        <Header />
        {data && data.map((item) => <MainContent message={item} />)}
    
        <Footer />
      </div>
    );
}

Let's say if there is any wrong data and one of the child components inside the loop gets an error, then the whole page errors,

what I want to avoid whole page error just to show a message of that place of child component where the error occurred.

This is just a sample of what I am trying to achieve. I know I can handle the error of a key like below

{prop.message.message?.substring(1, 5)}

But with actual data, we cannot be sure where the error may occur.

[sample code link][1]

I am using TypeScript in my project

Thanks

[1]: https://stackblitz.com/edit/react-parent-child-component-i4tc25?file=src/MainContent.js
英文:

Let's say I have a child component

function MainContent(prop) {
return (
&lt;main&gt;
&lt;p&gt;{prop.message.message.substring(1, 5)}&lt;/p&gt;
&lt;/main&gt;
);
}

and rendering it in parent like

function App() {
const data = [
{
message: &#39;first message&#39;,
},
{
message: &#39;second message&#39;,
},
{
//message: &#39;third message&#39;, working
wrongkey: &#39;third message&#39;, // not working
},
];
return (
&lt;div&gt;
&lt;Header /&gt;
{data &amp;&amp; data.map((item) =&gt; &lt;MainContent message={item} /&gt;)}
&lt;Footer /&gt;
&lt;/div&gt;
);
}

Let's say if there is any wrong data and one of the child component inside the loop gets error then the whole page errors,

what I want to avoid whole page error just to show a message of that place of child component where the error occurred.

This is just a sample what I am trying to achieve, I know I can handle error of a key like below

{prop.message.message?.substring(1, 5)}

But with actual data, we cannot be sure where error may occur.

sample code link

I am using Typescript in my project

Thanks

答案1

得分: 1

你可以使用条件运算符,在数据丢失时显示错误:

function MainContent(prop) {
  return (
    <main>
      <p>{
        prop.message.message
        ? prop.message.message?.substring(1, 5)
        : "Error text"
      }
      </p>
    </main>
  );
}
英文:

You can use conditional operator and show error in case the data is missing:

function MainContent(prop) {
return (
&lt;main&gt;
&lt;p&gt;{
prop.message.message
? prop.message.message?.substring(1, 5)
: &quot;Error text&quot;
}
&lt;/p&gt;
&lt;/main&gt;
);
}

huangapple
  • 本文由 发表于 2023年4月4日 11:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925296.html
匿名

发表评论

匿名网友

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

确定