I have a component that renders if some conditions are met, otherwise it returns null
.
I'd like to know how to determine if the component is returning null
from its parent.
I have tried logging the component to see what properties are changing when it is rendered or when returning null
but can not detect any difference.
Any suggestions?
To check for null in React, use a comparison to check if the value is equal or is not equal to null , e.g. if (myValue === null) {} or if (myValue !== null) {} . If the condition is met, the if block will run. Copied!
To check if a variable is null or undefined in React, use the || (or) operator to check if either of the two conditions is met. When used with two boolean values the || operator returns true if either of the conditions evaluate to true . Copied!
If you need to check if a prop was passed to a component outside of its JSX code, compare the prop to undefined . Copied! const Button = ({withIcon}) => { if (withIcon !== undefined) { console.
When you say null means you are not passing any props to that component. ~~ type in React. createElement can be a string like h1 , div etc or a React-Component.
You can just use a fallback prop.
Instead of:
function Child(){
if(something) return null
return <div>content</div>
}
function Parent(){
// try to find out if child is null
return <Child />
}
Just do:
function Child({ fallback = null }){
if(something) return fallback
return <div>content</div>
}
function Fallback() {
return 'some fallback'
}
function Parent(){
return <Child fallback={<Fallback />} />
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With