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?
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.
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.
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.
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.
I see 2 errors:
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
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With