Javascript Flux/Alt未从存储更新道具

Javascript Flux/Alt未从存储更新道具,javascript,reactjs,flux,alt,immutable.js,Javascript,Reactjs,Flux,Alt,Immutable.js,我正在尝试使用从商店收到的新道具重新发布我的React组件,但道具似乎没有更新。这种方法通常对我有效,只是在这种情况下不起作用 组件 class Cart extends React.Component { static getStores() { return [CartStore]; } static getPropsFromStores() { return CartStore.getState(); } com

我正在尝试使用从商店收到的新道具重新发布我的React组件,但道具似乎没有更新。这种方法通常对我有效,只是在这种情况下不起作用

组件

class Cart extends React.Component {

    static getStores() {
        return [CartStore];
    }

    static getPropsFromStores() {
        return CartStore.getState();
    }

    componentDidMount() { // tried componentWillMount also
        CartActions.updateCart();
    }

    render() {
        console.log(this.props.cart); // empty object

        return (
            <div className="cart">
                {this.props.cart.map((item, i) => <div key={i}>{item.get('name')}</div>)}
            </div>
        );
    }

}

export default connectToStores(Cart);
商店

class CartStore {

    constructor() {
        this.bindActions(CartActions);

        this.cart = Immutable.fromJS([]);
    }

    updateCart() {
        this.cart = Immutable.fromJS(JSON.parse(localStore.getItem('cart')));
        console.debug('cart', this.cart); // returns the correct object
    }

}

export default alt.createStore(CartStore, 'CartStore');
存储接收操作事件并从localStorage检索正确的对象。但是,组件的道具不会像通常那样更新

感谢您的帮助!谢谢:)

版本

class CartActions {

    updateCart() {
        this.dispatch();
    }

}

export default alt.createActions(CartActions);
  • 反应0.14.3
  • alt.js 0.17.9

您需要在视图中的某个位置聆听您的存储,通常是componentDidMount,如果您正在从存储中提取某个内容,并且该内容将发生更改,则该内容应该来自更高阶的组件,或者只是成为一种状态。在这种情况下,状态变量更适合

所以,像这样的东西会有用的

componentDidMount () {
  CartStore.listen(this.onChange)
  CartActions.updateCart()
}

onChange () {
  this.setState({
    xxx: xxxx
  }) // or change props like yours if you insist 
}

render() {
    return (
        <div className="cart">
            {this.state.cart.map((item, i) => <div key={i}>{item.get('name')}</div>)}
        </div>
    );
}
componentDidMount(){
CartStore.listen(this.onChange)
CartActions.updateCart()
}
onChange(){
这是我的国家({
xxx:xxxx
})//如果你坚持的话,也可以改变你的道具
}
render(){
返回(
{this.state.cart.map((item,i)=>{item.get('name')}
);
}

Flux实现alt.js正在为我做这件事。connectToStores方法解决了这个问题。正如我所说,如果我查询一个API,它就会工作。但对于localStore,它不知何故不起作用。这种方法确实有效,但它阻止了真正的同构渲染,在服务器和客户端上生成了不同的哈希。这个问题很老,但我现在也有同样的问题,我正在使用connectToStores,我可以看到存储接收到操作,但视图没有更新状态。