I am using React-native, axios after i learn how to install jwt and apply to my React native i see without state management i have to pass jwt token that fetch from login auth as props down multiple lvl that what i start hating. so i research and see about redux, redux-thunk, redux-saga
and i choose redux-saga
.
Can any guide me to setup redux-sagas like Folder structure, code split example i had 2 page like product, firmware.
in redux-thunk
action/
/actionProduct.js
/actionFirmware.js
reducer/
/index.js
/productReducer.js
/firmwareReducer.js
store.js
index.js
but in redux-sagas i'm start to confuse it has action, reducer, sagas and store is difference setup too.
const sagaMiddleware = createSagaMiddleware();
// mount it on the Store
const store = createStore(reducer, applyMiddleware(sagaMiddleware));
// then run the saga
sagaMiddleware.run(mySaga);
export default store;
in redux-thunk i never see simillar syntax like this sagaMiddleware.run(mySaga);
Do i need to create sagas folder and add 2 sagas like productSagas, firmwareSagas ?
Do i need to write 2 line like
sagaMiddleware.run(productSagas);
sagaMiddleware.run(firmwareSagas);
export default store;
or we can setup dynamic store ? and how can i do it ? Thank in advanced.
New Topic
i'm not sure i have setup this properly or not let see my setup. Store.js
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
rootReducer,
window.devToolsExtension && process.env.NODE_ENV !== 'production' ?
compose(
applyMiddleware(sagaMiddleware),
window.devToolsExtension(),
) :
applyMiddleware(sagaMiddleware),
);
sagaMiddleware.run(rootSaga);
export default store;
rootSaga file
export default function* rootSaga() {
yield all([
firmwareWatcher(),
productWatcher(),
]);
}
rootReducer
export default const rootReducer = combineReducers({
firmware,
porduct,
});
App.js
class App extends Component {
componentDidMount() {
const { dispatch } = this.props;
dispatch({ type: "FIRMWARE_REQUEST"});
dispatch({ type: 'PRODUCT_REQUEST'});
}
....
}
const mapStateToProps = (state) => {
console.log(state);
return { ... };
};
this is console.log(state) inside mapStateToProps i get and i consone.log in switch case to know ACTION TYPE
PRODUCT_SUCCESS
{type: "PRODUCT_SUCCESS", payload: {…}, @@redux-saga/SAGA_ACTION: true}
{firmware: {…}, porduct: {…}}
FIRMWARE_SUCCESS
{type: "API_CALL_SUCCESS", payload: {…}, @@redux-saga/SAGA_ACTION: true}
default
{firmware: {…}, porduct: {…}}
{firmware: {…}, porduct: {…}}
|
V
firmware : {fetching: false, dog: null, error: null, data: "https://dog.ceo/api/img/retriever-golden/n02099601_2495.jpg"}
porduct :
data : {id: 120, name: "A1", image: "a1_12-16-2017-1513389068.jpg", price: "28.00", status: 1, …}
sagaMiddleware.run(IndexSagas);
right ? – Quarles