amos-redux
v0.2.4
Published
`amos-redux` allows you to use `amos` and `redux` at the same time without modifying your redux business.
Downloads
32
Readme
amos-redux
amos-redux
allows you to use amos
and redux
at the same time without modifying your redux business.
Feature
- Allows you to use
amos
'store.select()
(you can use it withamos
'useSelector()
in react function components) to select bothamos
' andredux
's state at the same time, and useamos
'store.dispatch()
(you can get it byamos
'useDispatch()
in react function components) to dispatch bothamos
' dispatchable things andredux
's actions. - Keeps the
amos
's outstanding features:selector caches
andtransactions
, you can get more detailed information about tis inQuick start
's example.
Installation
yarn add amos-redux
# or var npm
npm i -S amos-redux
Quick start
You can get a full online demo here:
The following code is the simplified:
import { createStore as createReduxStore } from 'redux';
import { Provider as ReduxProvider } from 'react-redux';
import {
createStore as createAmosStore,
Provider as AmosProvider,
useSelector,
useDispatch,
} from 'amos';
import { withRedux } from 'amos-redux';
const reduxStore = createReduxStore(rootReducer);
const amosStore = createAmosStore(void 0, withRedux(reduxStore) /* hybrid redux store */);
ReactDOM.render(
<ReduxProvider store={reduxStore}>
<AmosProvider store={amosStore}>
<MyApp />
</AmosProvider>
</ReduxProvider>,
document.querySelector('#root'),
);
function MyApp() {
const [amosState, reduxState] = useSelector(
selectSomeAmosState,
selectSomeReduxState /* select redux's state with amos' useSelector */,
);
const dispatch = useDispatch();
const handleAmosClick = () => dispatch(someAmosAction());
const handleReduxClick = () => dispatch(someReduxAction()); // dispatch redux's action with amos' dispatch
return (
<div>
<div>
<span>Amos state: {amosState} </span>
<button onClick={handleAmosClick}>click me</button>
</div>
<div>
<span>Redux state: {reduxState} </span>
<button onClick={handleReduxClick}>click me</button>
</div>
</div>
);
}
API Reference
withRedux(reduxStore)
import { Store } from 'redux';
import { StoreEnhancer } from 'amos';
function withRedux(reduxStore: Store): StoreEnhancer;
Parameters
reduxStore
: the redux store you injected into your project
Returns
A StoreEnhancer
which is used for amos createStore()
.
License
The MIT License (MIT)
Copyright (c) 2020 acrazing
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.