2017-09-07 46 views
0

我創建了一個驗證動作,看起來像這樣:終極版商店更新沒有出現在道具

export const validate = (requestObject, validationObject) => { 

    return (dispatch) => { 

    // First, clear any existing errors 
    dispatch(clearErrors()); 

    // Call validation function 
    const response = validateObjectFunction(requestObject, validationObject); 

    // Set state for isValid 
    dispatch(setIsValid(response.isValid)); 

    // Set error messages 
    if(!response.isValid) dispatch(setErrors(response.errors)); 

    } 
} 

const setIsValid = (value) => { 
    return { 
     type: types.SET_IS_VALID, 
     value 
    }; 
} 

在我的組件我的處理函數,我只需撥打該驗證動作,然後根據狀態isValid,我打電話給我的後端API。

handleClickSubmit() { 

    // Form the request object for validation 
    const request = { 
     id: this.props.id, 
     name: this.props.name 
    }; 

    // Grab the validation object defined inside the compoenent that contains all validation requirements and call validate action. 
    this.props.actions.validate(request, validationsObject); 

    // This is where the issue is. 
    // Although I know for sure, I hit the reducer and set the state of isValid to TRUE, when I get here the isValid is still FALSE 
    if(this.props.isValid) this.props.actions.callApiFunction(request); 
} 

在我的測試中,我第一次只需點擊提交,以便爲isValid的狀態設置爲FALSE。然後我將數據輸入到表單中的所有必填字段中,然後再次提交。

作爲代碼執行,我看到我打的減速,並設置isValid爲TRUE擊中線if(this.props.isValid) this.props.actions.callApiFunction(request);

我提起,我沒有這個過程中任何async這一點很重要了。一切都發生在記憶中。

任何想法可能會導致isValid的狀態不夠快地更新。

回答

0

道具在redux生命週期中不會更新。我假設你在mapStateToProps中設置道具?

在調用validate函數之後,您可能可以將狀態值取出狀態,但我可能會建議將其移入操作創建者而不是組件。

+0

這整個事情發生在動作創建者內部,除了處理函數。我不明白爲什麼道具不會更新。他們直接從商店讀書。 – Sam

+0

對,你的組件中尚未更新處理函數所在的道具。這應該在行動中處理。 – ovation22

+0

如果您查看動作創建者代碼,我還會在其中設置錯誤。我的組件獲取錯誤作爲道具也他們更新fine.i不明白爲什麼只有這一個道具不更新。 – Sam