makeCollectionHoax factory.
Example
import React, { useLayoutEffect, useEffect, Fragment } from "react";
import { makeCollectionHoax } from "react-hoax";
const {
Provider,
useCollection,
useMember,
useSelector,
useAction,
useResourceSelector
} = makeCollectionHoax("myCustomResource", {
resourceOptions: {
initialState: {
title: "",
description: "",
keywords: [],
industry: "",
offer: ""
}
},
idKey: 'title'
});
const MyCustomResourceName = ({title}) => {
const [name, setName, error, setError] = useMember({resourceId: title, fieldKey: 'name'});
return (
<Fragment>
{name}
<button onClick={() => setName('FOO')}>change name to "FOO"</button>
</Fragment>
);
}
const MyCustomResourcesEntry = ({ children }) => {
const startFetch = useAction("startFetch");
const doneFetch = useAction("doneFetch");
const [loaded, loading, ids] = useSelector(state => [state.loaded, state.loading, state.ids])
const theDescriptionForGivenTitle = useResourceSelector('A title', state => state.description)
useLayoutEffect(() => {
if (!loaded) startFetch();
}, [loaded]);
useEffect(() => {
if (loading) {
promise.resolve([{title: 'A title', description: 'something'}])
.then(data => doneFetch(data));
}
}, [loading])
if (loading) return "Loading...";
return <Fragment>{ids.map(title => <MyCustomResourceName key={id} title={title} />}</Fragment>;
};
const MyCustomResourcesProvider = () => (
<Provider>
<MyCustomResourcesEntry />
</Provider>
);
export {
useCollection,
useMember,
useSelector as useMyCustomResourcesSelector,
useAction as useMyCustomResourceAction,
useResourceSelector as useMyCustomResourceSelector
};
export { queries };
export default MyCustomResourcesProvider;
Members
(private, static, constant) initialState
- Source:
- Default Value:
- { loading: true, loaded: false, processing: false, byId: {}, ids: [] }
Methods
(private, static) makeReducer(getInitialState, customReducer) → {collectionHoax.ReducerWithInit}
- Source:
Parameters:
Name | Type | Description |
---|---|---|
getInitialState |
function | a function that returns the initialState |
customReducer |
function | a custom reducer |
Returns:
(private, static) makeUseSelector(StateCtx, DispatchCtx) → {collectionHoax.Selectors}
- Source:
Parameters:
Name | Type | Description |
---|---|---|
StateCtx |
object | the context with the member state |
DispatchCtx |
object | the context with the member dispatches / actions |
Returns:
Type Definitions
ReducerWithInit
- Source:
Properties:
Name | Type | Description |
---|---|---|
reducer |
function | the reducer to be used along with provider |
init |
function | the function to initialize the reducer's state |
Type:
- Object
Selectors
- Source:
Properties:
Name | Type | Description |
---|---|---|
useSelector |
function |
|
useResourceSelector |
function |
|
useAction |
function |
|
Type:
- Object