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

feat: add useComponentWillReceiveUpdate #24

Merged
merged 10 commits into from
Oct 11, 2024
61 changes: 61 additions & 0 deletions docs/src/pages/use-component-will-receive-update.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: useComponentWillReceiveUpdate
---

# useComponentWillReceiveUpdate

import ExportMetaInfo from '../components/export-meta-info';

<ExportMetaInfo />

Change states based on changed props during a re-render. The name of the hook comes from [`UNSAFE_componentWillReceiveProps` method of class components](https://react.dev/reference/react/Component#unsafe_componentwillreceiveprops).

## Usage

```js
import { useComponentWillReceiveUpdate } from 'foxact/use-abortable-effect';

function Component(props) {
const [a, setA] = useState('')
const [b, setB] = useState('')
// when props.x changed, only reset a, not b
useComponentWillReceiveUpdate(() => {
setA('')
}, [props.x]);
}
```

If you're using useEffect like this:

```js
const [state, setState] = useState(false)
useEffect(() => setState(false), [props.someProp])
```

Don't do it. See [Adjusting some state when a prop changes](https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes) or [Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders)
It should be like this:
```js
const [prev, setPrev] = useState(state)
if (prev !== state) {
setPrev(state)
setState(false)
}
```

This hook is a helper for the above pattern.

```js
useComponentWillReceiveUpdate(() => setState(false), [state])
```

This only applies to states of the current component.
Modifying states from other components causes React reporting errors.
You may also want to read [(Avoid) Notifying parent components about state changes](https://react.dev/learn/you-might-not-need-an-effect#notifying-parent-components-about-state-changes) and [(Avoid) Passing data to the parent](https://react.dev/learn/you-might-not-need-an-effect#passing-data-to-the-parent).
If you need to edit other components' states, write it like this:
SukkaW marked this conversation as resolved.
Show resolved Hide resolved

```js
useComponentWillReceiveUpdate(() => {
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
setLocalState(false)
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
Promise.resolve().then(() => props.setParentState(false))
Jack-Works marked this conversation as resolved.
Show resolved Hide resolved
}, [state])
```
52 changes: 52 additions & 0 deletions src/use-component-will-receive-update/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useState } from 'react';

/**
* If you're using useEffect like this:
*
* ```js
* const [state, setState] = useState(false)
* useEffect(() => setState(false), [props.someProp])
* ```
* Don't do it. See [Adjusting some state when a prop changes](https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes)
* It should be like this:
* ```js
* const [prev, setPrev] = useState(state)
* if (prev !== state) {
* setPrev(state)
* setState(false)
* }
* ```
* This hook is a helper for the above pattern.
* ```js
* useComponentWillReceiveUpdate(() => setState(false), [state])
* ```
*
* This only applies to states of the current component.
* Modifying states from other components causes React reporting errors.
* You may also want to read [(Avoid) Notifying parent components about state changes](https://react.dev/learn/you-might-not-need-an-effect#notifying-parent-components-about-state-changes)
* and [(Avoid) Passing data to the parent](https://react.dev/learn/you-might-not-need-an-effect#passing-data-to-the-parent).
* If you really need to edit other components' states, write it like this:
*
* ```js
* useComponentWillReceiveUpdate(() => {
* setLocalState(false)
* Promise.resolve().then(() => props.setParentState(false))
* }, [state])
* ```
*
* @param callback
* @param deps
*/
export function useComponentWillReceiveUpdate(callback: () => void, deps: readonly unknown[]) {
deps = [...deps];
const [prev, setPrev] = useState(deps);
let changed = deps.length !== prev.length;
for (let i = 0; i < deps.length; i += 1) {
if (changed) break;
if (prev[i] !== deps[i]) changed = true;
}
if (changed) {
setPrev(deps);
callback();
}
}