我真的很挣扎,但我无法让我的组件重新渲染,即使我可以看到商店正在发生变化,是的,我不确定我没有改变我的原始状态。
减速器:
const AssessmentReducer = (state = initialState, action) => {
switch (action.type) {
case GET_ASSESSMENT_INFO:
return { ...state, assessment: action.payload }
default:
return state
}
}
操作:
import { GET_ASSESSMENT_INFO } from '../reducers/AssessmentReducer'
import API from '../../api'
export function getAssessment(assessment) {
return {
type: GET_ASSESSMENT_INFO,
payload: assessment,
}
}
export function getAssesmentInfo(id) {
return function (dispatch) {
return API.get_all('assessments/' + id).then(
(response) => dispatch(getAssessment(response)),
(error) => console.log('An error occurred', error)
)
}
}
组件:
componentDidMount() {
const assessmentId = this.props.match.params.assessment_id
this.props.getAssesmentInfo(assessmentId)
}
/** A whole bunch of stuff in here ***/
render() {
console.log(this.props.assessment)
}
function mapStateToProps(state) {
return {
//screenQuestions: state.ScreenQuestionsReducer.screenQuestions,
assessment: state.AssessmentReducer.assessment,
}
}
export default connect(mapStateToProps, { getAssesmentInfo })(
withStyles(styles)(AssessmentTabs)
)
我看到我的初始状态被记录在渲染中,可以验证操作被调用并使用正确的有效负载分派到减速器。但是渲染中的控制台日志永远不会再被击中。使用 redux 开发工具,状态也被正确更新。
有什么想法吗?
编辑:
只要在我的组件中单击一个按钮并更新状态,它就会从 redux 中读取新的 props 没问题