javascript - "Updating" all state in redux app properly -


i've met trouble assigning new object in reducer of app. state contains 2 arrays :

{     elements: [],     constraints: [] } 

those elements handled 2 reducers :

elementsreducer constraintsreducer 

and combined this:

let reducer = combinereducers({     elements: elementsreducer,     constraints: constraintsreducer });  export default reducer 

so, basically, action triggered, , reducer supposed update state.elements array. i've tried several things , can't update whole elements array, - in best case - first element.

my first idea do:

        return  object.assign({}, state, {             elements: state.map((e) => {                 return object.assign({}, e, {                     text: action.data[e.id][e.text]                 })             })         }); 

action.data array containing different text each element. basically, is, on special action, updating element array. syntax not work creates new array inside array "elements" of store. not replace it. if let this, store becomes:

{     elements: [          elements: [...]     ],     constraints: [...] } 

when access state in reducer elementsreducer, it's "element" array , not full state. after issue, i've tried following:

return state.map(function(e) {     return assign({}, e, {           text: action.data[e.id][e.text]    }); }); 

now, worked, element mapped first one. other elements not updating.

do have idea solve issue?

thanks :) xelys

edit :

// code of elementsreducer var assign = require('object-assign');  export default function elementsreducer(state = {}, action) {     switch (action.type) {         case 'add_element':             return [...state,                 {                     name: action.name,                     id: action.id,                     committed: false,                     text: action.text                 }             ]         case 'commit_element':             console.log('commit action')             return state.map(function(e) {                 return e.id === action.id ?                 assign({}, e, {committed: true}) :                 e             });         case 'save_data':             return state.map((e) => {                 return object.assign({}, e, {                     text: action.data[e.id][e.text]                 });             });         default:             return state;     } } 

based on code, assumed data structure below:

// state.element stateelement = [     { id:1, text: '1t' },     { id:2, text: '2t' } ];  // action result action = {     data: {         1: { text: 'new 1t' },         2: { text: 'new 2t' }     } }  // new state.element newdata = data.map(function(e) {     return object.assign({}, e, {           text: action.data[e.id].text    }); }); 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -