admin管理员组文章数量:1430566
Consider I got two ponents:
// Hello.js
import React from "react";
export default function Hello(props) {
const { name = "mike" } = props;
return <div>Hello {name}</div>;
}
// App.js
import React from "react";
import Hello from "./Hello";
export default function App() {
return (
<div>
<Hello name={null} />
</div>
);
}
// renders Hello
If I pass null
props to the Hello
ponent, The Hello
ponent will not use the default value, instead, it will just use null
as a value and render it.
The default value will only be used when i explicitly pass undefined
or not pass anything:
<Hello name={undefined} />
<Hello />
// renders Hello mike
So my question is How should i handle null props correctly? Should i handle it in parent ponent when pass it to child ponent like this:
// App.js
<Hello name={getName() || 'mike'} />
Or i should handle it in child ponent like this:
// Hello.js
return <div>Hello {name || "mike"}</div>;
Is there any "best practice" to help handle this?
Consider I got two ponents:
// Hello.js
import React from "react";
export default function Hello(props) {
const { name = "mike" } = props;
return <div>Hello {name}</div>;
}
// App.js
import React from "react";
import Hello from "./Hello";
export default function App() {
return (
<div>
<Hello name={null} />
</div>
);
}
// renders Hello
If I pass null
props to the Hello
ponent, The Hello
ponent will not use the default value, instead, it will just use null
as a value and render it.
The default value will only be used when i explicitly pass undefined
or not pass anything:
<Hello name={undefined} />
<Hello />
// renders Hello mike
So my question is How should i handle null props correctly? Should i handle it in parent ponent when pass it to child ponent like this:
// App.js
<Hello name={getName() || 'mike'} />
Or i should handle it in child ponent like this:
// Hello.js
return <div>Hello {name || "mike"}</div>;
Is there any "best practice" to help handle this?
Share Improve this question edited Nov 24, 2020 at 7:37 Sarun UK 6,7667 gold badges26 silver badges50 bronze badges asked Nov 24, 2020 at 6:54 LimboerLimboer 4146 silver badges33 bronze badges2 Answers
Reset to default 3You can use JS null coalescing operator to achieve in a below way:
props.name ?? 'Default value'
bad, cos somebody else will forget and yell WTF
// App.js
<Hello name={getName() || 'mike'} />
so so, but this is ugly, assign default value is more elegant
// Hello.js
return <div>Hello {name || "mike"}</div>;
better, why not just return string or undefined
function getName(){
...
return something || undefined
}
best, people wont always do so, how to make sure? TypeScript will check it when pile
interface Props {
name: string;
}
interface State {
xxx: xxx
}
class Hello extends React.Component<Props, State> {
choose as you like, cos best costs most
本文标签: javascriptBest practice to handle null props in ReactStack Overflow
版权声明:本文标题:javascript - Best practice to handle null props in React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745468941a2659659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论