Currently I'm reading the react beta official document.
When I read this part, I'm very confused about the definition of rendering a component in different positions.
// Code from official document
export default function Scoreboard() {
const [isPlayerA, setIsPlayerA] = useState(true);
return (
<div>
// If we use this pattern, the Counter state will not be preserved
{isPlayerA &&
<Counter person="Taylor" />
}
{!isPlayerA &&
<Counter person="Sarah" />
}
// If we use this pattern, the Counter state will be preserved
{/*isPlayerA ? <Counter person="Taylor" /> : <Counter person="Sarah" />*/}
<button onClick={() => {
setIsPlayerA(!isPlayerA);
}}>
Next player!
</button>
</div>
);
}
function Counter({ person }) {
...
}
In the above example, the state in the Counter will be reset because the document said that they will be mount in different positions. However, I'm confused that why they're in different positions? and why another conditional rendering pattern(see the comment above) can preserve state? What's the difference between them?
I've also experimented something, but get confused even more.
export default function Scoreboard() {
const [isPlayerA, setIsPlayerA] = useState(true);
return (
<div>
{isPlayerA && <div>Up</div>}
{isPlayerA ? <Counter person="Taylor" /> : <Counter person="Sarah" />}
{!isPlayerA && <div>Down</div>}
<button onClick={() => {
setIsPlayerA(!isPlayerA);
}}>
Next player!
</button>
</div>
);
}
In the above experiment, the Counter state is preserved also. However, Counter component will be in different position when the isPlayer
state changed.