react-context-store
Advanced tools
Comparing version 1.0.0-beta.6 to 1.0.0
@@ -10,2 +10,6 @@ # Changelog | ||
## [1.0.0] - 2021-03-02 | ||
- Fixed react peer dependency range | ||
## [1.0.0-beta.6] - 2021-03-01 | ||
@@ -12,0 +16,0 @@ |
@@ -15,3 +15,3 @@ { | ||
"enzyme-adapter-react-16": "1.15.6", | ||
"husky": "5.1.2", | ||
"husky": "5.1.3", | ||
"jest": "26.6.3", | ||
@@ -31,3 +31,3 @@ "lint-staged": "10.5.4", | ||
"peerDependencies": { | ||
"react": "^16.0.0 || ^17.0.0" | ||
"react": "^16.8.0 || ^17.0.0" | ||
}, | ||
@@ -47,3 +47,3 @@ "repository": { | ||
"sideEffects": false, | ||
"version": "1.0.0-beta.6" | ||
"version": "1.0.0" | ||
} |
101
readme.md
@@ -7,4 +7,30 @@ # React context store | ||
## Basic usage | ||
## API | ||
There are a few types of store hooks both of which return the current store contents and the available modifiers. | ||
- useContextStore - For non-indexable stores like objects and primatives or when you don't need to edit individual elements | ||
- useIndexableContextStore - For indexable stores like maps or arrays with a single loading state at the root | ||
- useIndexableStatefulContextStore - For indexable stores but when you want to maintain separate load states per item than the list of items | ||
All come with some basic modifiers: | ||
- useUpdateFactory - Used to modify all the values in the store at once. Useful for getAll, deleteAll, modifyAll, etc. | ||
- setContextData - Used to create custom modifiers if for some reason you don't like ours. | ||
They also have the same states: | ||
- unsent - No modifier has acted upon the data | ||
- loading - The modifier has been invoked but the action hasn't completed yet | ||
- success - The action was successful and the data has been updated | ||
- error - The action failed | ||
The `useIndexableContextStore` and `useIndexableStatefulContextStore` have additional update factories that allow for creating functions that manipulate individual items: | ||
- useCreateOneFactory - Used to create a new entry - GET and POST calls. | ||
- useDeleteOneFactory - Used to remove an entry - DELETE call | ||
- useUpdateOneFactory - Used to update an existing entry - PUT/PATCH calls | ||
## Example usage | ||
The following documentation has been copied out of a test case. If you find that it's not working, please check the test case. This example is an array but you can also use maps, or store a non-indexable type. | ||
@@ -19,5 +45,5 @@ | ||
ContextStore, | ||
getNotImplementedPromise, | ||
useIndexableContextStore, | ||
getNotImplementedPromise, | ||
} from "@tesla/react-context-store"; | ||
} from "react-context-store"; | ||
@@ -29,7 +55,7 @@ export type Item = { | ||
type ContextStoreDataArray = Array<Item>; | ||
type ContextStoreData = Array<Item>; | ||
export type GetAllParams = ContextStoreDataArray; | ||
export interface ContextValue extends ContextStore<ContextStoreDataArray> { | ||
getAll: (params: GetAllParams) => Promise<ContextStoreDataArray>; | ||
export type RefreshAllParams = void; | ||
export interface ContextValue extends ContextStore<ContextStoreData> { | ||
refreshAll: (params: RefreshAllParams) => Promise<ContextStoreData>; | ||
} | ||
@@ -39,3 +65,3 @@ | ||
data: [], | ||
getAll: getNotImplementedPromise, | ||
refreshAll: getNotImplementedPromise, | ||
state: "unsent", | ||
@@ -45,19 +71,13 @@ }; | ||
export const Context = React.createContext(defaultValue); | ||
export type ApiProviderProps = PropsWithChildren<{}>; | ||
export type ProviderProps = PropsWithChildren<{}>; | ||
export function ApiProvider(props: ApiProviderProps) { | ||
export function ApiProvider(props: ProviderProps) { | ||
const { children } = props; | ||
const [ | ||
contextValue, | ||
{ | ||
useUpdateFactory, | ||
useUpdateOneFactory, | ||
useCreateOneFactory, | ||
useDeleteOneFactory, | ||
}, | ||
] = useIndexableContextStore(defaultValue); | ||
const [contextValue, { useUpdateFactory }] = useIndexableContextStore( | ||
defaultValue | ||
); | ||
const getAll = useUpdateFactory({ | ||
action: (params: GetAllParams) => { | ||
return await fetchList(params); | ||
const refreshAll = useUpdateFactory({ | ||
action: (params: RefreshAllParams) => { | ||
return fetchResults(params); | ||
}, | ||
@@ -68,3 +88,6 @@ }); | ||
<Context.Provider | ||
value={{ ...contextValue, getAll }} | ||
value={{ | ||
...contextValue, | ||
refreshAll, | ||
}} | ||
> | ||
@@ -98,6 +121,6 @@ {children} | ||
export function List() { | ||
const { data, getAll, state } = useContext(Context); | ||
const { data, refreshAll, state } = useContext(Context); | ||
useEffect(() => { | ||
getAll(); | ||
refreshAll(); | ||
}); | ||
@@ -126,29 +149,1 @@ | ||
For more examples, take a look at our extensive testing suite. | ||
## API | ||
There are two types of store hooks both of which return the current store contents and the available modifiers. | ||
- useContextStore - For non-indexable stores like objects and primatives | ||
- useIndexableContextStore - For indexable stores like maps or arrays | ||
- useStatefulIndexableContextStore - For indexable stores but when you want to maintain separate load states per item than the list of items | ||
Both come with the following modifiers: | ||
- useUpdateFactory - Used to modify all the values in the store at once. Useful for getAll, deleteAll, modifyAll, etc. | ||
- setContextData - Used to create custom modifiers if for some reason you don't like ours. | ||
They also have the same states: | ||
- unsent - No modifier has acted upon the data | ||
- loading - The modifier has been invoked but the action hasn't completed yet | ||
- success - The action was successful and the data has been updated | ||
- error - The action failed | ||
The `useIndexableContextStore` has an additional update methods that affect individual items each of which have their own state: | ||
- useUpdateOneFactory - Used to create an updater which modifies an existing entry - PUT calls | ||
- useCreateOneFactory - Used to create a creator which creates a new entry - GET and POST calls. | ||
- useDeleteOneFactory - Used to create a deletor which removes an entry - DELETE call | ||
The `useStatefulIndexableContextStore` has the same methods as above however each item in the index will have a load state. When any of the `use___OneFactory` functions are used, the list's load state doesn't change however the individual item's load state does. |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
102923
142