A place to keep your disposable but application-related component state data
Sometimes you write components that hold a state with data related to your application's data, in an ideal world you would like to keep all of your's app state in the redux state, but sometimes these components dont have an unique key in the redux state because there's an undefined number of instances of your component across the app.
The main goal is to register a unique sub-state for each component that needs it into the redux state, this happens when the component mounts/unmounts.
The data in this state, very much like the data in the component's local state, isn't persistent and is completely discarded in the unmount lifecycle.
Instead of initializing your component's state in its constructor you would do as you would with a redux reducer declaring its initial state in the reducer's definition.
// declaration of your Autocomplete.js component
const Autocomplete = ({ options }) => (
...
)
const mapStateToProps = (state /* this is the componet's unique state */) => {
return {
options: state.options
}
}
export default connectUUID('autocomplete', mapStateToProps)(Autocomplete);
// using it somewhere else
<div>
<Autocomplete />
<Autocomplete />
<Autocomplete />
</div>
// each of the Autocomplete components opens up a new key in your app's state
uuid: {
autocomplete: {
// autogenerated uuids by this lib
'bc1de127-0962-43a6-a224-9cc4716da4b6': {...},
'cc94ccb8-85a3-407a-9fc0-cc895459b422': {...},
'41952137-e54c-4f9e-be08-07ffe11ee554': {...}
}
}
Creates your UUID reducer, make sure to place its result state under state.uuid
reducers
(Object<key, reducer>): An object map of reducers, where each<key>
sets the reducer's name (seeconnectUUID(name, ...args)
)
A reducer that will filter out redux actions that go through it to the corresponding reducer
const mainAppReducer = combineReducers({
uuid: createUUIDReducer({
counter: counterReducer,
fizzbuzz: fizzbuzzReducer
})
})
Creates a HoC to connect your component to it's corresponding reducer state, its very similar to
react-redux's connect
. It will inject the uuid
prop to the component as well.
this.props.uuid
(String): The component's uuid
name
(String): The name of the corresponding reducer that this HoC will use from the reducer object map passed on tocreateUUIDReducer
in the main reducer declaration[mapStateToProps]
: See react-redux's docs[mapDispatchToProps]
: See react-redux's docs
A higher-order React component that injects the inner state and wrapped action creators into your component.
IMPORTANT NOTE: if a component connected this way is provided an uuid
prop then the component
wont automatically generate its own UUID and it wont unregister the UUID when unmounted, this can be
extremely useful when dealing with UUID states that are handled manually, see the examples in the
repo to understand how this works
Wraps calls to the given action creator, making it so it only applies to reducers within the given
name
, if the uuid
parameter is passed then it will only apply to the only reducer matching the
uuid (most times you wont need this).
actionCreator
(Function|Object): The action creator to be wrapped, if an object of actions is passed it will wrap all the actions within instead.name
(String): The name of the reducer that actions will apply to[uuid]
(String): The name of the uuid of the reducer that the action will apply to, you wont need to use this parameter this most of the time.
A new action (or object of actions) that will do the same as the action before but it will only apply to reducers that match the criteria.
An action creator for creating new sub-states into the given collection, useful when you wish to index objects using a custom key
name
(String): The name of the collection you wish to register a new sub-state inuuid
(String|Object<String, Any>): The UUID that matches the new sub-state, can also be an object where the keys are the new UUIDs and the values are the initial states for them
The action that once dispatched to the state would commit the change
An action creator for removing the sub-states into from given collection
name
(String): The name of the collection that contains the uuiduuid
(String|Array<String>): The UUID that matches the sub-state, can also be an array for batch updates
The action that once dispatched to the state would commit the change
A helper for selecting a specific sub-state
state
(Object): The whole redux statename
(String): The name of the collection of sub-statesuuid
(String): The uuid you're looking for
The sub-state corresponding the query
A helper for selecting an array of all the available keys in a collection
state
(Object): The whole redux statename
(String): The name of the collection of sub-states
An array of UUIDs that are currently registered into the collection