redux-thunk

how to `bindActionCreators` with redux-thunk

微笑、不失礼 提交于 2019-12-05 08:44:32
I am quite new to JavaScript and react-native and I have existing project that I need to add functionality to. It is using redux and redux-thunk with redux-saga to send API requests. Currently it supports only 1 dispatch function per component and I need to dispatch several types of requests to the saga. I am trying to bindActionCreators to add the dispatch to the stores but to no avail.. I am totally lost on the mapDispatchToProps part and how do I "fire the action" afterwards.. in single dispatch to props, I did this: let sdtp = (arg) => { return (dispatch) => { dispatch({ type: 'GET_TEST

React native router flux: TypeError: undefined is not a function (evaluating 'addListener')

情到浓时终转凉″ 提交于 2019-12-05 03:04:06
问题 Im working on a react native app using this principal dependencies: react native react native router flux react thunk expo I was working using this package.json: "dependencies": { "expo": "23.0.4", "humps": "^2.0.0", "install": "^0.10.1", "lodash": "^4.17.4", "native-base": "^2.3.5", "react": "16.0.0", "react-native": "0.50.4", "react-native-extend-indicator": "^0.1.2", "react-native-keyboard-aware-scroll-view": "^0.4.2", "react-native-maps": "^0.19.0", "react-native-maps-directions": "^1.3.0

Initialize component with Async data

馋奶兔 提交于 2019-12-04 23:43:08
I'm trying to figure out how and where to load the data (ie call dispatch on my action) for my select box in react + redux + thunk. I'm not sure if it should go in the constructor of my App container, or should i load it inside my component (in my example: "MyDropdown") My main App: import MyDropdown from '../components/mydropdown'; // Should i import my action here and then... // import { loadData } from '../actions'; class App extends Component { render() { return ( <div className="page-content"> <div className="option-bar"> // SEND it as a PROP inside MyDropdown... <MyDropdown /> </div> <

Firebase, react and redux wait for store to update

情到浓时终转凉″ 提交于 2019-12-04 19:57:33
I have a React app connected to Firebase, i want to check if user is logged in at every page refresh, dispatching IS_SIGNED_IN action, here's the action creator: export function checkAuth() { return dispatch => { firebaseAuth().onAuthStateChanged((user) => { if (user) { dispatch(signedInAction()) } else { dispatch(signedOutAction()) } }) } } Inside the reducer i just make boolean isAuthenticated to true/false: case ActionTypes.isSignedIn: { return Object.assign({}, state, { isAuthenticated: true }) } case ActionTypes.isSignedOut: { return Object.assign({}, state, { isAuthenticated: false }) }

Why use Redux Thunk [closed]

雨燕双飞 提交于 2019-12-04 19:56:48
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 10 months ago . Why use Redux Thunk then one can do something like this: ReadableAPI.getCategories().then((categories)=>{ console.log('after getCategories', categories) this.props.dispatch(addCategories(categories)) }) Isn't this more straightforward and achieves the same thing? 回答1: This

React/Redux store state is not saved

扶醉桌前 提交于 2019-12-04 17:54:44
I use thunk middleware and pass initial state to React, but the problem is that React state is not saved when I visit other links. After successfully logged in, it's supposed to render dashboard. User must be redirected to dashboard(which is the root path, / ) when he tries to go to /login page. Should i use redux-router too? I omitted some of code, but it almost looks like below. init.js I passed store to Provider import { Provider } from 'react-redux'; import configureStore from './store/configureStore'; const store = configureStore(); function requireAuth(nextState, replace) { const

How do I resolve “Actions must be plain objects. Use custom middleware for async actions.]”?

柔情痞子 提交于 2019-12-04 12:53:22
So I have wasted 5 hours on this. I have a redux thunk action like this: export const fetchUser = () => async (getState, dispatch) => { if (getIsFetching(getState().user)) { return Promise.resolve(); } dispatch(fetchUserRequest()); try { const response = await api.fetchUser(); dispatch(fetchUserSuccess({ userObject: { ...response } })); } catch (error) { dispatch(fetchUserFailure({ message: "Could not fetch user profile." })); } }; Calling this always ended up in "Actions must be plain objects. Use custom middleware for async actions.]". Yeah, sure. I'm already using redux-thunk for that, why

Redux thunk: return promise from dispatched action

旧城冷巷雨未停 提交于 2019-12-04 04:02:40
Is it possible to return promise/signal from action creator, resolved when Redux thunk has successfully dispatched certain action? Consider this action creator: function doPost(data) { return (dispatch) => { dispatch({type: POST_LOADING}); Source.doPost() // async http operation .then(response => { dispatch({type: POST_SUCCESS, payload: response}) }) .catch(errorMessage => { dispatch({type: POST_ERROR, payload: errorMessage}) }); } } I want to call some function asynchronously in the component after calling doPost action creator when Redux has either dispatched POST_SUCCESS or POST_ERROR

Cancelling previous async action using redux-thunk

偶尔善良 提交于 2019-12-04 01:14:40
I am building a React/Redux app using the redux-thunk middleware to create and handle Ajax requests. I have a particular thunk that is fired pretty often, and I would like to cancel any previously started Ajax requests before firing a new one. Is this possible? One approach would be to mark those requests as canceled by giving them random id and checking its status before handling the result. The way to do this is to assign random id for this call in your first dispatch (inside the thunk) and check it in the reducer before handling the result. const actionId = Math.random(); dispatch({type:

How do I test a Redux action creator that only dispatches other actions

断了今生、忘了曾经 提交于 2019-12-03 21:27:34
问题 I'm having trouble testing an action creator that just loops through the array passed to it and dispatches an action for each item in that array. It's simple enough I just can't seem to figure it out. Here's the action creator: export const fetchAllItems = (topicIds)=>{ return (dispatch)=>{ topicIds.forEach((topicId)=>{ dispatch(fetchItems(topicId)); }); }; }; And here's how I'm attempting to test it: describe('fetchAllItems', ()=>{ it('should dispatch fetchItems actions for each topic id