2017-01-10 32 views
0

當我嘗試從ajax調用中分派動作時,當存儲本身得到更新時(在瀏覽器的插件中檢查),組件不會不會收到該事件,因此不會重新呈現。當從ajax調用中分派動作時,React組件不會收到事件

class SomeComponent extends React.Component{ 
    constuctor (props) { 
    super(props); 
    this.someAjaxCall(); 
    } 
    someAjaxCall(){ 
     this.props.dispatch(someAction('Work')); 
      $.ajax({ 
     type: "POST", 
     url: "someURL", 
     success: function(data) 
     { 
      this.props.dispatch(someAction("Does Not Work")); 

     }.bind(this) 
    }); 
    } 
    render(){ 
    return (<div></div>); 
    } 
} 

const mapStateToProps = (store, ownprops) => { 
    return { 
     store: store.get("Some_Key") 
    } 
    } 


const render = (store,container) => { 
    let ConnectedComponent = ReactRedux.connect(this.mapStateToProps)(SomeComponent); 
     ReactDOM.render(
       <Provider store={ store }> 
        <ConnectedComponent/> 
       </Provider> 
     , container); 
} 

const someAction = (state) => { 
    return { 
    type: 'SOME_CASE', 
    state: state 
    } 
} 

const reducer = (state, action) => { 
    switch (action.type) { 
    case 'SOME_CASE':{ 
     return state.set("stateKey",action.state); 
    } 
    default: { 
     return state; 
    } 
    } 
} 

從上面的示例中,當我調用this.props.dispatch(someAction('Work'));

商店得到更新,組件獲取事件並重新呈現自己。但是,當我從ajax調用的「成功」函數內部調用時,商店被更新,但組件從不接收事件。

+0

你有沒有嘗試過在'componentDidMount'裏面調用'this.someAjaxCall()'而不是'constructor'? – oobgam

+0

我剛剛試過你的建議。不工作。同樣的行爲。第一次調度工作正常,但第二次調度僅將商店中的值存儲起來,並且組件不會被調用。 – user1432882

+0

什麼事件?你期望'this.props.dispatch(someAction(「不起作用」));'raise? – Hosar

回答

0

如果我錯了,請糾正我。 第二個Action創建者調用不應該失敗,因爲someAction不是全局的。你有沒有得到任何錯誤。

相關問題