I'm making Weather App using React + Redux, and I want to make opportunity to follow cities and save the information.
I guess, I need to make something like this in Reducer:
// state
const initialState = {
cityValue: '',
isLoading: false,
data: [], // array which contains data getting from ajax request
error: '',
followingCity: [] // new array with cities which are followed
}
//reducer's case
case ADD_CITY: {
return {
...state,
followingCity: [...state.followingCity, action.city]
}
}
// action creator
export const addCity = (city) => ({type: 'ADD_CITY'}, city)
Using connect, the component gets props and then:
<div className="col-lg-5">
{ data.map(i =>
<div className='time-box' key={i.id}>
{dateCreator(new Date())}
<br/>
<div className='country'>{i.sys.country},</div>
<div className='city'>{i.name}</div>
<br/>
<div onClick={() => addCity(data)} className='add-button'> //calling action
<span>follow</span>
<i className="fa fa-plus" aria-hidden="true"></i>
</div>
</div>)}
</div>
But in this case I have error:
Error: Actions must be plain objects. Use custom middleware for async actions.
So, I'm not sure that I have to make it like this, so can anybody expaine me how to make it?
mapDispatchToStoreexport const addCity = (city) => ({type: 'ADD_CITY', city})(ie move curly bracket to include city)