reactjs - Loop to dispatch a reducer in Redux -
i have json
file array of todos want render initial state calling reducer type: 'add_todo'
. better loop dispatch
action:
is better loop in component, adding each single todo, like:
myinitialtodos.foreach( (todo, id) => store.dispatch({ type: 'add_todo', id }) )
or in reducer
composed add_todo
, like:
store.dispatch({ type: 'add_todos', todos: myinitialtodos })
in both cases, launching when componentdidmount
(for example).
maybe (and probably) there , better way.
thanks,
its better add separate action type of add_todos , pass array of todos reducer. if think adding backend react application, wouldn't want make bunch of api calls add multiple todos, you'd want send 1 network request.
edit: also, if going want add same list of todos application start, better way put them in initial state pass reducer, trick:
const initialstate = [{title: 'a sample todo', completed: false},{title: 'another sample todo', completed: false}] const todos = (state = initialstate, action) => { //reducer logic }
Comments
Post a Comment