2017-10-29 3 views
2

나는 다음과 같은 오류지고있어 외부 관측 값을 변경 :가 Mobx 행동

proxyConsole.js:54 Error: [mobx] Invariant failed: Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: [email protected] 
    at invariant (mobx.module.js:2326) 
    at fail (mobx.module.js:2321) 
    at checkIfStateModificationsAreAllowed (mobx.module.js:2890) 
    at ObservableValue../node_modules/mobx/lib/mobx.module.js.ObservableValue.prepareNewValue (mobx.module.js:796) 
    at setPropertyValue (mobx.module.js:1673) 
    at Object.set [as items] (mobx.module.js:1641) 
    at Store.js:41 
    at <anonymous> 

을하지만 내가 action에서 함수를 포장하고 그래서 조금 혼란 스러워요 :

import { observable, useStrict, action } from 'mobx'; 
import Services from './Services'; 

// ... 

getAllTodos: action(() => { 

    Services.getAllTodos() 
    .then((response) => { 

     state.items = response.data; 

    }).catch((error) => { 
     console.error(error); 
    }); 

}), 

서비스 .js

// ... 

getAllTodos() { 
    return axios.get(root + '/items/'); 
} 

무엇이 여기에 있습니까?

답변

2

을 관찰 가능한 변화시키는 기능 action 래핑해야하므로 역시 콜백을 사용

getAllTodos: action(() => { 

    Services.getAllTodos() 
    .then(action((response) => { 
    state.items.replace(response.data); 
    })).catch((error) => { 
    console.error(error); 
    }); 
})