42
loading...
This website collects cookies to deliver better user experience
createEntityAdapter
method, which has a very nice API to perform operation in a stateful data. So, I thought about wrapping the createEntityAdapter
method with a hook which provides all the method required to perform state manipulation.@redux/toolkit
that helps to maintain CRUD operation of the state.useStatefulAdapter
provides an API to manipulate the state without worrying about handling all the states.const [state, handler, { selectById }] = useStatefulAdapter<{
id: string;
text: string;
}>({
name: 'my-adapter',
selectId: (item) => item.id,
});
npm i use-stateful-adapter
yarn add use-stateful-adapter
import * as React from 'react';
import useStatefulAdapter from 'use-stateful-adapter';
export default function App() {
const [state, handler, { selectById }] = useStatefulAdapter<{
id: string;
text: string;
}>({
name: 'my-adapter',
selectId: (item) => item.id,
});
}
useStatefulAdapter
returns [currentState
,handler
,selectors
addOne
: Add one entity to the collectionaddMany
: Add multiple entities to the collectionaddAll
: Replace current collection with provided collectionremoveOne
: Remove one entity from the collectionremoveMany
: Remove multiple entities from the collection, by id or by predicateremoveAll
: Clear entity collectionupdateOne
: Update one entity in the collectionupdateMany
: Update multiple entities in the collectionupsertOne
: Add or Update one entity in the collectionupsertMany
: Add or Update multiple entities in the collectionmap
: Update multiple entities in the collection by defining a map function, similar to Array.mapselectById(id:string):void
: Select item by IDimport * as React from 'react';
import useStatefulAdapter from '../src';
export default function App() {
const [state, handler, { selectById }] = useStatefulAdapter<{
id: string;
text: string;
}>({
name: 'my-adapter',
selectId: (item) => item.id,
});
const [currentId, setCurrentId] = React.useState<string | null>(null);
const [todo, setTodo] = React.useState('');
const handleSubmit = React.useCallback(
(e) => {
e.preventDefault();
if (currentId) {
handler.updateOne({
id: currentId,
changes: {
text: todo,
},
});
setCurrentId(null);
} else {
handler.addOne({
id: String(Math.random()),
text: todo,
});
}
setTodo('');
},
[handler, todo]
);
const currentValue = React.useMemo(() => {
return selectById(currentId!);
}, [currentId]);
React.useEffect(() => {
if (!currentValue) return;
setTodo(currentValue.text);
}, [currentValue]);
return (
<form onSubmit={handleSubmit} className="App">
<input
key={currentId}
name="todo"
value={todo}
onChange={(e) => setTodo(e.currentTarget.value)}
placeholder="Add Todo"
type="text"
/>
<button type="button" onClick={handler.removeAll}>
Remove All
</button>
{currentId && <div>Currently editing {currentId}</div>}
{state.map((item) => (
<React.Fragment key={item.id}>
<li>{item.text}</li>
<button type="button" onClick={() => handler.removeOne(item.id)}>
Delete
</button>
<button type="button" onClick={() => setCurrentId(item.id)}>
Edit
</button>
</React.Fragment>
))}
</form>
);
}