Created
August 30, 2019 05:33
-
-
Save huwshimi/0f95fa8a77874851370a4a49a2aca3ae to your computer and use it in GitHub Desktop.
take() and useSelector()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { applyMiddleware, createStore } from "redux"; | |
import { composeWithDevTools } from "redux-devtools-extension"; | |
import { delay, take } from "redux-saga/effects"; | |
import { useDispatch, useSelector, Provider } from "react-redux"; | |
import createSagaMiddleware from "redux-saga"; | |
import React, { useEffect } from "react"; | |
import ReactDOM from "react-dom"; | |
const sagaMiddleware = createSagaMiddleware(); | |
const composeEnhancers = composeWithDevTools({}); | |
const middleware = [sagaMiddleware]; | |
const enhancers = composeEnhancers(applyMiddleware(...middleware)); | |
const store = createStore(() => {}, enhancers); | |
function* rootSaga() { | |
yield delay(1); | |
while (true) { | |
const { type } = yield take(); | |
console.log("type", type); | |
} | |
} | |
sagaMiddleware.run(rootSaga); | |
const App = () => { | |
useSelector(state => [], () => false); // Change the final value to true and the two actions will be received by the take() in the saga. | |
const dispatch = useDispatch(); | |
useEffect(() => { | |
dispatch({ | |
type: "FIRST_ACTION" | |
}); | |
dispatch({ | |
type: "SECOND_ACTION" | |
}); | |
}, [dispatch]); | |
return <div>App</div>; | |
}; | |
ReactDOM.render( | |
<Provider store={store}> | |
<App /> | |
</Provider>, | |
document.getElementById("root") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment