Currently I want to dispatch the action inside the async function, but the action is not being dispatched? The reason for using Redux is because I want to add a like button to any images that is being filtered by the search input.
The link to the entire App am working on.... ---go to file components/App.js
import React from 'react';
import SearchBar from './SearchBar';
import unsplash from '../api/unsplash';
import ImageList from './ImageList';
import { fetchImages } from '../actions';
import { connect } from 'react-redux';
class App extends React.Component {
state = { images: [] };
onSearchSubmit = term => {
return async (dispatch) => {
const response = await unsplash.get('/search/photos', {
params:{ query: term },
//use the path on unsplash for image searching
})
let success = response.data.results
dispatch(fetchImages(success));
return success;
// this.setState({ images: response.data.results });
}
}
render() {
return (<div className="ui container" style={{marginTop:'10px'}}>
<SearchBar onSubmit={this.onSearchSubmit}/>
<ImageList images={this.state.images}/>
</div>);
}
}
const mapDispatchToProps = (dispatch) => ({
fetchImages: images => dispatch(fetchImages(images))
})
export default connect(null, mapDispatchToProps)(App);
dispatchis not magic keyword, you would have to passdispatchto yourAppor your function inside your App componentonSearchSubmitso that it holds reference todispatchusing which you can dispatch actions in there. - Rikin