suggest add some code examples here, I think it's very helpful for people who are learning
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
https://react.dev/learn/preserving-and-resetting-state#preserving-state-for-removed-components

for example:
You could render all chats instead of just the current one, but hide all the others with CSS.
import { useState } from 'react';
import Chat from './Chat.js';
import ContactList from './ContactList.js';
export default function Messenger() {
const [to, setTo] = useState(contacts[0]);
return (
<div>
<ContactList
contacts={contacts}
selectedContact={to}
onSelect={(contact) => setTo(contact)}
/>
<div className="chats-container">
{contacts.map((contact) => (
<div
key={contact.id}
className={`chat-wrapper ${to.id === contact.id ? 'active' : ''}`}
>
<Chat contact={contact} />
</div>
))}
</div>
<style>
{`
.chats-container {
display: flex;
}
.chat-wrapper {
display: none;
}
.chat-wrapper.active {
display: block;
}
`}
</style>
</div>
);
}
const contacts = [
{ id: 0, name: 'Taylor', email: 'taylor@mail.com' },
{ id: 1, name: 'Alice', email: 'alice@mail.com' },
{ id: 2, name: 'Bob', email: 'bob@mail.com' },
];

Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked “Preserving state for removed components” section on react.dev and review the supplied Messenger example and screenshot. Add a clear code example explaining the suggested approach, then verify that it fits the surrounding documentation and renders correctly in the page.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100