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

Introduce a new hook that called useStateRef #201

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions text/0000-use-state-ref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
- Start Date: 2021-08-24
- RFC PR: (leave this empty)
- React Issue: (leave this empty)

# Summary

Introduce a new hook to reduce/improve some processes.

# Basic example

In this example we see `useEffect` that doesn't need a dependency to read updated `count`'s value, so that's mean we don't need to perform `useEffect` effect function event `count` will change. `useStateRef` just is a name, so might we need to change to a better name.

```js
const [count, setCount, getCount] = useStateRef();

useEffect(() => {
const handleVisibility = () => {
console.log(getCount());
};

document.addEventListener('visibilitychange', handleVisibility);

return () => document.removeEventListener('visibilitychange', handleVisibility);
}, []);
```

Also, in this example, `useCallback` doesn't need a dependency to know `count`'s value, so `useCallback` inside function just performs once. that's awesome

```js
const [count, setCount, getCount] = useStateRef();

const handleClick = useCallback(() => {
console.log(getCount());
}, []);
```

# Motivation

`useEffect` and `useCallback` need to know dependencies to redefine `function` depended on new values of dependencies. but these are redundant process for `React` because we don't know what time we need it exactly ex: `handleClick` depend on user behavior so we just need `count`'s value when the user needs it.

# Detailed design

A basic of implementation for `useStateRef` that we can use in project is:

TS version:

```js
function useStateRef<T>(initialValue: T): [T, (nextState: T) => void, () => T] {
const [state, setState] = useState(initialValue);
const stateRef = useRef(state);
stateRef.current = state;
const getState = useCallback(() => stateRef.current, []);
return [state, setState, getState];
}
```

JS version:

```js
function useStateRef(initialValue) {
const [state, setState] = useState(initialValue);
const stateRef = useRef(state);
stateRef.current = state;
const getState = useCallback(() => stateRef.current, []);
return [state, setState, getState];
}
```

# Drawbacks

This is a new hook, so we don't have any breaking change, also we can implement that by internal React hooks.

# Alternatives

Alternative can be a package, maybe

# Adoption strategy

Fortunately, we don't have any breaking change, also we can embed this hook to `useState` without breaking change

```js
const [count, setCount, getCount] = useState();
```

# How we teach this

This RFC introduce a new hook, so we have to add some section to React documentation

# Unresolved questions

- do we need to implement a new hook or embed it to `useState`?