React (JavaScript library): Difference between revisions

From David's Wiki
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Components==
Components are reusable pieces of jsx which depend on some input properties and get rerendered whenever those properties change.
React has two types of components: function components and class components (those which extend React.Component).
Historically, class components were used for components with state and function components were stateless.
Today, function components can also have state using hooks and should be preferred for new code.
===Function components===
====State====
https://beta.reactjs.org/learn/managing-state
State can be managed in several ways:
* [https://beta.reactjs.org/reference/react/useState <code>useState</code>] allows you to define a state variable, similar to a class property, which will trigger a rerender.
* [https://beta.reactjs.org/reference/react/useReducer <code>useReducer</code>] allows you to refractor state manipulation to a separate ''reducer'' function outside of your component.
* [https://beta.reactjs.org/reference/react/useEffect <code>useEffect</code>] allows you to setup and remove listeners. These always run on the client in SSR variants of React.
* Contexts
* [https://beta.reactjs.org/reference/react/useRef#useref <code>useRef</code>] returns a reference to an object with a <code>current</code> parameter which allows you to have state without rerendering.
Every time any properties or state changes, your function component will be rerendered.
Therefore, you cannot rely on local variables to hold their state.
Additionally, you must call <code>useState</code> identically across renders (i.e. not conditionally), ideally near the top of your function.
Note that the state of the function is tied to their position in the React virtual dom.
A single component rendered multiple times will have different state.


==Frameworks==
==Frameworks==

Latest revision as of 03:55, 8 February 2023

Components

Components are reusable pieces of jsx which depend on some input properties and get rerendered whenever those properties change.

React has two types of components: function components and class components (those which extend React.Component). Historically, class components were used for components with state and function components were stateless. Today, function components can also have state using hooks and should be preferred for new code.

Function components

State

https://beta.reactjs.org/learn/managing-state State can be managed in several ways:

  • useState allows you to define a state variable, similar to a class property, which will trigger a rerender.
  • useReducer allows you to refractor state manipulation to a separate reducer function outside of your component.
  • useEffect allows you to setup and remove listeners. These always run on the client in SSR variants of React.
  • Contexts
  • useRef returns a reference to an object with a current parameter which allows you to have state without rerendering.

Every time any properties or state changes, your function component will be rerendered. Therefore, you cannot rely on local variables to hold their state. Additionally, you must call useState identically across renders (i.e. not conditionally), ideally near the top of your function.

Note that the state of the function is tied to their position in the React virtual dom. A single component rendered multiple times will have different state.

Frameworks

Next.js

Next.js allows you to use React with server-side rendering.

Resources