-
Notifications
You must be signed in to change notification settings - Fork 2
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
[FEATURE]: add react best practices #15
Open
padupuy
wants to merge
1
commit into
main
Choose a base branch
from
feature/react-best-practices
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,121 @@ | ||
# React Best Practices | ||
|
||
## Main best practices | ||
|
||
- [Think in React](https://react.dev/learn/thinking-in-react) | ||
- Use only Function Components, [no more Class Component](https://react.dev/reference/react/Component#alternatives) | ||
- Use custom hooks instead of HOC, replace withFoo by useFoo | ||
- Use composition | ||
- Use slots | ||
- Use "as" prop to override an `Element` | ||
- Seperate the smart and dumb component | ||
- Keep state as local as possible. | ||
- Derive state, don't sync state, (you might not need an effect)[https://react.dev/learn/you-might-not-need-an-effect] | ||
- Prefer state enums instead of booleans | ||
- Use Storybook to code your components in isolation and enforce the separation of concern design pattern | ||
- Use React Testing Library for testing your components and mimic users effects | ||
- Use the Web Platform as much as possible | ||
|
||
## State Management | ||
|
||
Read [Managing State](https://react.dev/learn/managing-state) | ||
|
||
> When you want to coordinate two components, move their state to their common parent. | ||
> Then pass the information down through props from their common parent. | ||
> Finally, pass the event handlers down so that the children can change the parent’s state. | ||
> It’s useful to consider components as “controlled” (driven by props) or “uncontrolled” (driven by state). | ||
|
||
If you are sure that `useState`, `useReducer` nor `useContext` cannot meet your needs, then you can choose an external tool from the following list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. excellent! |
||
|
||
Data Management : | ||
|
||
- [react-query](https://tanstack.com/query/v3) | ||
|
||
State Management : | ||
|
||
- [Jotai](https://jotai.org) | ||
- [Mobx](https://mobx.js.org) | ||
- [Zustand](https://zustand.docs.pmnd.rs/getting-started/introduction) | ||
|
||
State Machine : | ||
|
||
- [xstate](https://xstate.js.org) | ||
|
||
Deprecated : | ||
|
||
- Redux & redux-saga | ||
|
||
## Hooks | ||
|
||
Hooks feature was introduced in React 16.8 that allow you to use state and other React features in functional components, which were previously only available in class components. | ||
Hooks are functions that let you "hook into" React state and lifecycle features from function components. They provide a more direct API to the React concepts you already know. | ||
Hooks make React components more functional and modular. | ||
|
||
### Custom Hooks | ||
|
||
You can create your own Hooks to extract and share logic between components. Custom Hooks are a convention that naturally follows from the design of Hooks, rather than a React feature. | ||
|
||
## Form | ||
|
||
Forms are a crucial part of Web applications and the Web Platform offers many options for managing forms natively. | ||
HTML5 validation features can cover a lot of your needs. | ||
If you need more fine grained validation, most form-related tasks can be handle by hooks like `useState` and `useEffect`, see [State Management](#state-management)). | ||
|
||
If you are sure that `useState`, `useReducer` nor `useContext` cannot meet your needs, then you can choose an external tool from the following list : | ||
|
||
- [React Hook Form](https://react-hook-form.com) | ||
- [Formik](https://formik.org) | ||
|
||
Resources : | ||
|
||
- Use key to reset a state, ie for (reseting a form)[https://react.dev/learn/preserving-and-resetting-state#resetting-a-form-with-a-key] | ||
- [useId()](https://reacttraining.com/blog/use-useid-instead-of-hand-making-ids) | ||
|
||
## Performance | ||
|
||
@TODO | ||
|
||
Resources : | ||
|
||
- (`<Profiler>`)[https://react.dev/reference/react/Profiler] | ||
- (compiler)[https://react.dev/learn/react-compiler] | ||
|
||
## Client component | ||
|
||
@TODO @React19 | ||
|
||
## Server component | ||
|
||
@TODO @React19 | ||
|
||
## Server function | ||
|
||
@TODO @React19 | ||
|
||
## Tools | ||
|
||
### Linter | ||
|
||
Resources : | ||
|
||
- [eslint-plugin-react](https://www.npmjs.com/package/eslint-plugin-react) | ||
|
||
### Debug | ||
|
||
Resources : | ||
|
||
- [React Developer Tools](https://react.dev/learn/react-developer-tools) | ||
|
||
### Tests | ||
|
||
Resources : | ||
|
||
- [jest](https://jestjs.io) | ||
- [vitest](https://vitest.dev) | ||
- [React Testing Library](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library) | ||
- [react-hooks-testing-library](https://github.com/testing-library/react-hooks-testing-library) | ||
- [msw](https://mswjs.io) | ||
|
||
## Misc | ||
|
||
- [React](https://react.dev) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about display name?