reactjs / reactjs/react.dev

suggest add some code examples here, I think it's very helpful for people who are learning

Open
#5,947 0 comments 0 reactions 0 assignees View on GitHub

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
image
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' },
];

Memo3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.