Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React 18+] Tips for new version of React #82

Open
reboottime opened this issue May 3, 2024 · 1 comment
Open

[React 18+] Tips for new version of React #82

reboottime opened this issue May 3, 2024 · 1 comment

Comments

@reboottime
Copy link
Owner

reboottime commented May 3, 2024

General tips

  • Group related state

    • Bad Practice:
       const [x, setX] = useState(0);
       const [y, setY] = useState(0);
    • Good practice
          const [position, setPosition] = useState({x:0, y:0});
    • The why: you won’t forget to always keep them in sync
  • Best practice on organizating state

    • flat state
@reboottime
Copy link
Owner Author

reboottime commented May 3, 2024

State Structure Tips

  • Group related state

    • Bad Practice:
       const [x, setX] = useState(0);
       const [y, setY] = useState(0);
    • Good practice
          const [position, setPosition] = useState({x:0, y:0});
    • The why: you won’t forget to always keep them in sync
  • Avoid contradictions in state: In short, Prefer to use state machine

  • Avoid redundant state:

    • Bad:
       const [firstName, setFirstName] = useState("");
       const [lastName, setLastName] = useState("");
       const [fullName, setFullName] = useState('');
    • Good:
        const [firstName, setFirstName] = useState("");
        const [lastName, setLastName] = useState("");
        
        const fullName = `${firstName} ${lastName}`
  • Avoid duplication in state: ( do not store the same information at two places)

    • Bad: copy cuts an object and store its whole content
    • Good: store the identifier
  • Avoid deeply nested state: example

    • ben:
      • code maintainbility
      • performance benefits: reduce memory usage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant