2
アクションをディスパッチするのではなく、ストアの値を直接還元するとどうなりますか?ここではサンプルコードストアの値を直接更新しないのはなぜですか?
:
import actions from '../actions'
class Example extends Component {
static contextTypes = {
store: PropTypes.any,
}
static propTypes = {
drawer: PropTypes.object,
}
componentDidMount() {
const { drawer } = this.props
const { store } = this.context
// I can update 'drawer' values directly and "it works" as following
// drawer.unread -= 1
// Also I can update 'drawer' values by dispatching an action as following
drawer.unread -= 1
this.context.store.dispatch(actions.updateDrawer(drawer))
}
}
function mapStateToProps(state) {
return {
drawer: state.drawer,
}
}
export default connect(mapStateToProps)(Example)
'uiObjectProp'変更に再描画したい他のコンポーネントがある場合、ディスパッチを使用しない限り、それらは変更されません。状態は本当に変わることはありません。 – Daniel
私はそう思ったが、彼らは変わる。私は遭遇した状況が例外的かどうかは疑問です。私は私の質問を更新します。 – efkan
@efkan、面白いです。私のアプリの店の一部だった深く入れ子にされたオブジェクトで同じことを試してみましたが、何も起こらなかったし、ドキュメントごとに「その内部の状態を変更する唯一の方法は、アクションをディスパッチすることです。私は周りにフィドル! – semuzaboi