Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling one action from another action creator

Tags:

I'm working on a Redux app in which many filter components can change the nature of a search to be performed. Any time the state of one of those filter components changes, I want to re-run a search action. I can't seem to call the search action from each of the filter components correctly, however.

Here's the main search action:

// actions/search.js import fetch from 'isomorphic-fetch'; import config from '../../server/config';  export const receiveSearchResults = (results) => ({   type: 'RECEIVE_SEARCH_RESULTS', results })  export const searchRequestFailed = () => ({   type: 'SEARCH_REQUEST_FAILED' })  export const fetchSearchResults = () => {   return (dispatch, getState) => {     // Generate the query url     const query = getSearchQuery();  // returns a url string     return fetch(query)       .then(response => response.json()         .then(json => ({           status: response.status,           json         })       ))       .then(({ status, json }) => {         if (status >= 400) dispatch(searchRequestFailed())         else dispatch(receiveSearchResults(json))       }, err => { dispatch(searchRequestFailed()) })   } } 

fetchSearchResults works fine when I call it from connected React components. However, I can't call that method from the following action creator (this is one of the filter action creators):

// actions/use-types.js import fetchSearchResults from './search';  export const toggleUseTypes = (use) => {   return (dispatch) => {     dispatch({type: 'TOGGLE_USE_TYPES', use: use})     fetchSearchResults()   } } 

Running this yields: Uncaught TypeError: (0 , _search2.default) is not a function. The same happens when I run dispatch(fetchSearchResults()) inside toggleUseTypes.

How can I resolve this problem and call the fetchSearchResults method from the actions/use-types.js action?

like image 786
duhaime Avatar asked Jan 02 '18 20:01

duhaime


People also ask

Can Redux action dispatch another action?

Dispatch actions using Redux-saga library To run many async instructions one after the other and also maintain readability of the code, we can dispatch an action that will then trigger a saga. We can use redux-saga library in the same way. Using the saga, we can dispatch actions with the put effect.

What is the difference between action and action creator?

An action is simply an object that has two things: a type, and a payload. An action creator is simply a function, that just returns an action.

How do I dispatch action from reducer?

Dispatching an action within a reducer is an anti-pattern. Your reducer should be without side effects, simply digesting the action payload and returning a new state object. Adding listeners and dispatching actions within the reducer can lead to chained actions and other side effects.

What is the difference between an action and an action creator in Redux?

Action creator is function which create and return function dynamically. Show activity on this post. An action in Redux is only a function. That it.


1 Answers

I see 2 errors:

  1. You're importing the fetchSearchResults function incorrectly.

    This is where the TypeError _search2.default is coming from:

    Uncaught TypeError: (0 , _search2.default) is not a function 
  2. You're dispatching the fetchSearchResults action/thunk incorrectly

Error 1: Incorrect import

// This won't work. fetchSearchResults is not the default export import fetchSearchResults from './search'; // Use named import, instead. import {fetchSearchResults} from './search'; 

Error 2: Incorrect action usage

// This won't work, it's just a call to a function that returns a function fetchSearchResults() // This will work. Dispatching the thunk dispatch(fetchSearchResults()) 
like image 57
Cory Danielson Avatar answered Oct 09 '22 15:10

Cory Danielson