Strange behavior of conditional rendering when bypassing props in functional component
See original GitHub issueThis is a bug I’ve got today. I have a parent component, that includes a nested child component with a prop passed into it. Prop named the same way as a variable, from which I get the value (idk if this is important). Prop is used later to conditional render some JSX block. In the first cycle, component gets this prop in a correct way: null passed and JSX block didn’t render. In the next cycle, when prop gets a value insted of null, it also works. But when finally prop got a null agian, like in the beginning, condition doesn’t work proper, block still appears.
Code trivia:
//doesn't work proper
const Parent = ({someProp}) => {
//some code here...
return (
<Child someProp={someProp}/>
);
};
const Child = ({someProp}) => (
{someProp ? (<SomeJsx/>) : (null)}
);
//works great
const Parent = ({someBigProps}) => {
var someProp = someBigProps.someProp;
//some code here...
{someProp ? (<SomeJsx/>) : (null)}
};
Real code shots:
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Six methods to achieve conditional rendering in React - Flexiple
We use an if with our condition and return the element to be rendered. Let's observe the example below. Consider these two components:....
Read more >How to prevent a component from rendering ? - GeeksforGeeks
When UI is designed using React, we come across a situation when components are to be rendered on the screen based upon some...
Read more >Conditional Rendering - React
Conditional Rendering. In React, you can create distinct components that encapsulate behavior you need. Then, you can render only some of them, depending...
Read more >reactjs - With useEffect, how can I skip applying an effect upon ...
Think of them as primarily rendering a component based on some props. If you genuinely need some local state, then useState will provide...
Read more >5 Ways to Implement Conditional Rendering in React
When null is returned by a component it prevents React from mounting the component. function App(props) { if(props.noRender) return null return ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can you try changing your code as below. Can you also try to reproduce the issue is jsfiddle.
Try debugging code it will also help.
Since there’s been no follow up or reproducible example, I’m going to go ahead and close this out.