Thanks for the feedback! I'm not sure if I understand you reply correctly, but here is demo that shows the child component will not be rendered unless the condition is true.
codesandbox.io/s/if-component-demo...
export default function App() { const [data, setData] = useState(null); return ( <div className="App"> {data ? <Child name="A" /> : <Child name="NO DATA" />} <If condition={data}> <Child name="Has Data" /> </If> <If condition={data}> <ChildNeedsData data={data} /> </If> </div> ); } function Child(props) { useEffect(() => { console.log(`Child ${props.name} is rendered`); return () => console.log(`Child ${props.name} is unmounted`); }, []); console.log(`Child ${props.name} is rendering`); return <div>Child {props.name}</div>; } function ChildNeedsData(props) { return <h4>{props.data.name}</h4>; }
Output
Child NO DATA is rendering Child NO DATA is rendered // No Error while running is demo
If you change the code to this:
<If condition={!data}> <ChildNeedsData data={data} /> </If>
You will get a error.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for the feedback!
I'm not sure if I understand you reply correctly, but here is demo that shows the child component will not be rendered unless the condition is true.
codesandbox.io/s/if-component-demo...
Output
If you change the code to this:
You will get a error.