question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange behavior of conditional rendering when bypassing props in functional component

See original GitHub issue

This 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:

image

image

image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mrgurdeepcommented, Feb 21, 2018

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.

const Child = ({someProp}) => {
	if (!someProp) { 
		return null;
	} 
	return <SomeJsx/>;
};
0reactions
awearycommented, Mar 27, 2018

Since there’s been no follow up or reproducible example, I’m going to go ahead and close this out.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found