-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d65e9ab
commit ce4603e
Showing
15 changed files
with
209 additions
and
172 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# useLegacyState | ||
|
||
This hook works similar to `this.setState` works in react class components. Here when you call `setState`, it shallow merges state partial into current state. It will be useful when you want change a class component to functional component. | ||
|
||
<pre>{`import {useLegacyState} from 'react-use-custom-hooks';`}</pre> | ||
|
||
### Usage example | ||
|
||
```typescript | ||
const [state, setState] = useLegacyState({ firstName: 'John' }); | ||
|
||
setState({ lastName: 'Doe' }); // state -> { firstName: 'John', lastName: Doe } | ||
``` | ||
|
||
### Playground | ||
|
||
```jsx live | ||
function LegacyStateExample(props) { | ||
const [state, setState] = useLegacyState({ firstName: 'John' }); | ||
|
||
return ( | ||
<> | ||
<div>state - {JSON.stringify(state, null, 2)} </div> | ||
<input | ||
type="text" | ||
placeholder="Last name" | ||
onChange={e => setState({ lastName: e.target.value })} | ||
/> | ||
<br /> | ||
<input | ||
type="number" | ||
placeholder="Age" | ||
onChange={e => setState({ age: e.target.value })} | ||
/> | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
### API | ||
|
||
```typescript | ||
function useSetState<T extends Record<string, any>>( | ||
initialState: T | ||
): readonly [ | ||
T, | ||
(statePartial: Partial<T> | ((currentState: T) => Partial<T>)) => void | ||
]; | ||
``` |
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
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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import styles from './HookDetails.module.css'; | ||
|
||
export default function HookDetails() { | ||
return ( | ||
<section className={styles.features}> | ||
<div className="container"> | ||
|
||
<h3 className={styles.red}> | ||
Installation | ||
</h3> | ||
<pre> | ||
npm install --save react-use-custom-hooks | ||
<br /> | ||
yarn add react-use-custom-hooks | ||
</pre> | ||
<h3 className={styles.red}> | ||
Usage | ||
</h3> | ||
<pre> | ||
{`import {useSafeState} from 'react-use-custom-hooks';`} | ||
</pre> | ||
</div> | ||
</section> | ||
); | ||
} |
Oops, something went wrong.