An open API service indexing awesome lists of open source software.

https://github.com/zhangyu1818/use-derived-value

Implement getDerivedStateFromProps with hook。
https://github.com/zhangyu1818/use-derived-value

react react-hooks

Last synced: 9 months ago
JSON representation

Implement getDerivedStateFromProps with hook。

Awesome Lists containing this project

README

          

# use-derived-value

![npm-version](https://img.shields.io/npm/v/use-derived-value.svg)
[![codecov](https://codecov.io/gh/zhangyu1818/use-derived-value/branch/main/graph/badge.svg?token=XMOY7SVSJ4)](https://codecov.io/gh/zhangyu1818/use-derived-value)

Implement getDerivedStateFromProps with hook。

[![Edit dark-pond-osbps](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/dark-pond-osbps?fontsize=14&hidenavigation=1&theme=dark)

## Install

```shell
npm install use-derived-value
```

## Usage

```jsx
import React from 'react'
import useDerivedValue from 'use-derived-value'

const Input = props => {
const defaultValue = props.defaultValue ?? props.value
const [state, setState] = useDerivedValue(defaultValue, {
postState: () => props.value ?? null,
onChange: props.onChange,
})

return
}

function App() {
const [value, setValue] = React.useState('initValue')
return (
{
setValue(target.value)
}}
/>
)
}
```

## API

```typescript
useDerivedValue(initialState: State, options: Options)

type Options = {
postState?: () => State | null
onChange?: (value: State, prevValue: State) => void
}
```

`postState` is a function, return a new value, if return null mean that is not controlled, it will use `state` in `useDerivedValue`.

`onChange` will called when set new value, second parameter is the old value.