redux-thunk

What are the benefits of using thunk middleware in redux over using regular functions as async action creators? [closed]

随声附和 提交于 2019-11-29 10:32:58
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I've been using redux for about two months now and have just recently started getting into exploring different ways of dealing with asynchronous behavior such as fetching data. It appears from the documentation and from discussions on GitHub that the standard way of doing this by

React Native Redux: Action dispatched, return results

核能气质少年 提交于 2019-11-28 12:02:22
问题 In redux, when an action is dispatched, reducer will change the state accordingly, the component which have called the action, also have access to the state ( passed through props by Provider ). Am I right? is the state the only way to access results of the action in the component? ( the component which have called the action ). How about passing a callback function to the action, and using that to send the result back to the component? 回答1: In redux, when an action is dispatched, reducer

How to dispatch multiple actions in react-navigation?

牧云@^-^@ 提交于 2019-11-28 05:09:50
问题 I want to perform two actions but it doesn't work. const backAction = NavigationActions.back({ key: null }) const dispatchLogoutAction = NavigationActions.reset({ index: 0, actions: [NavigationActions.navigate({routeName: "LoginView"})] }); nextProps.navigation.dispatch(backAction); nextProps.navigation.dispatch(dispatchLogoutAction); What other ways to dispatch both actions in react-navigation rather than listing dispatch statements parallelly? 来源: https://stackoverflow.com/questions

Can I dispatch multiple actions without Redux Thunk middleware?

孤街醉人 提交于 2019-11-28 05:01:04
I read that Redux Thunk is the reliable way to manage asynchronous actions/request. There's nothing much about dispatching actions by other actions. How about dispatching synchronous actions? I am not sure of thunk approach's performance issues, but can I just dispatch action inside other action creator without defining function inside? It seems to me that using redux thunk is unnecessary for this need. Dan Abramov Showing and hiding notification does indeed appear to be a good use case for thunks. David’s answer describes the “default” way of doing several updates in response to something:

Testing dispatched actions in Redux thunk with Jest

纵饮孤独 提交于 2019-11-28 03:23:36
问题 I'm quite new to Jest and admittedly am no expert at testing async code... I have a simple Fetch helper I use: export function fetchHelper(url, opts) { return fetch(url, options) .then((response) => { if (response.ok) { return Promise.resolve(response); } const error = new Error(response.statusText || response.status); error.response = response; return Promise.reject(error); }); } And implement it like so: export function getSomeData() { return (dispatch) => { return fetchHelper('http:/

What is the difference between redux-thunk and redux-promise?

隐身守侯 提交于 2019-11-27 16:51:28
As far as I know and correct me if I am wrong, redux-thunk is a middleware which helps us dispatch async function and debug values in the action itself while when I used redux-promise I couldn't create async functions without implementing my own mechanism as Action throws an exception of dispatching only plain objects. What is the major differences between these two packages? Are there any benefits of using both the packages in a single page react app or sticking to redux-thunk would be enough? redux-thunk allows your action creators to return a function : function myAction(payload){ return

how to async/await redux-thunk actions?

て烟熏妆下的殇ゞ 提交于 2019-11-27 14:24:21
问题 action.js export function getLoginStatus() { return async(dispatch) => { let token = await getOAuthToken(); let success = await verifyToken(token); if (success == true) { dispatch(loginStatus(success)); } else { console.log("Success: False"); console.log("Token mismatch"); } return success; } } component.js componentDidMount() { this.props.dispatch(splashAction.getLoginStatus()) .then((success) => { if (success == true) { Actions.counter() } else { console.log("Login not successfull"); } });

Pros/cons of using redux-saga with ES6 generators vs redux-thunk with ES2017 async/await

亡梦爱人 提交于 2019-11-27 02:19:54
There is a lot of talk about the latest kid in redux town right now, redux-saga/redux-saga . It uses generator functions for listening to/dispatching actions. Before I wrap my head around it, I would like to know the pros/cons of using redux-saga instead of the approach below where I'm using redux-thunk with async/await. A component might look like this, dispatch actions like usual. import { login } from 'redux/auth'; class LoginForm extends Component { onClick(e) { e.preventDefault(); const { user, pass } = this.refs; this.props.dispatch(login(user.value, pass.value)); } render() { return (

return promise from store after redux thunk dispatch

坚强是说给别人听的谎言 提交于 2019-11-27 00:51:25
问题 I am trying to chain dispatches with redux thunk function simple_action(){ return {type: "SIMPLE_ACTION"} } export function async_action(){ return function(dispatch, getState){ return dispatch(simple_action).then(()=>{...}); } } How do I get the dispatch to return a promise from the store? MORE SPECIFICALLY: I am probably just not understanding something here, but in all the examples with redux-thunk , they call a separate async event (like fetch ), which obviously returns a Promise . What I

Can I dispatch multiple actions without Redux Thunk middleware?

一笑奈何 提交于 2019-11-27 00:44:34
问题 I read that Redux Thunk is the reliable way to manage asynchronous actions/request. There's nothing much about dispatching actions by other actions. How about dispatching synchronous actions? I am not sure of thunk approach's performance issues, but can I just dispatch action inside other action creator without defining function inside? It seems to me that using redux thunk is unnecessary for this need. 回答1: Showing and hiding notification does indeed appear to be a good use case for thunks.