我的道具在状态改变时不会更新。我没有在任何reducer中改变状态,并且一切正常,除了我的道具没有更新。
具有连接功能的主要组件,所有内容都通过道具传递给其他组件:
const mapStateToProps = state => ({
blocks: state.blocks,
quizes: state.quizes,
auth: state.auth,
courses: state.courses,
tasks: state.tasks
});
const mapDispatchToProps = dispatch => ({
authActions: bindActionCreators(authActions, dispatch),
quizActions: bindActionCreators(quizActions, dispatch),
blockActions: bindActionCreators(blockActions, dispatch),
courseActions: bindActionCreators(courseActions, dispatch),
taskActions: bindActionCreators(taskActions, dispatch)
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(BlockList);
我的测验减速器:
// @flow
import AnyAction from "redux";
import { ActionTypes } from "./../constants";
const initialState = {
rightAnswers: 0,
index: 0,
wrongAnswers: 0,
uri: null
};
const someReduceFunction = () => {};
export default function Quizes(state = initialState, action: AnyAction) {
switch (action.type) {
case ActionTypes.RIGHT_ANSWER:
return Object.assign({}, state, {
index: state.index + 1,
rightAnswers: state.rightAnswers + 1,
uri: "https://www.shareicon.net/data/256x256/2016/08/20/817720_check_395x512.png"
});
case ActionTypes.WRONG_ANSWER:
return Object.assign({}, state, {
index: state.index + 1,
wrongAnswers: state.wrongAnswers + 1,
uri: "https://www.cpronetwork.com/media/d10b9ef5fc45ce16ba12240e263a7d8b.png?preset=m-thumb"
});
default:
return state;
}
}
测验动作:
// @flow
import { ActionTypes } from "../constants";
export const rightAnswer = () => {
return {
type: ActionTypes.RIGHT_ANSWER
};
};
export const wrongAnswer = () => {
return {
type: ActionTypes.WRONG_ANSWER
};
};
Quiz.js: quizAction和测验都是通过道具传递的:
const { navigation } = this.props;
const { task, image, quiz, rightAction, block, quizes, quizActions } = navigation.state.params;
const { rightAnswers, index, wrongAnswers, uri } = quizes;
console.log(quizes);
const current = quiz.questions[index];
const percentage = (rightAnswers / quiz.numberOfQuestions) * 100;
.
.
.
<Button
style={styles.button}
label={current.option1}
onPress={() => {
if (current.option1 === current.rightAnswer) {
quizActions.rightAnswer();
} else {
quizActions.wrongAnswer();
}
}}
/>
console.log(quizs)打印出初始状态。然后什么也不打印。
在console.logs中运行和reducer文件,我知道状态正在改变。只是道具不是。
我不确定这可能是我所有应用程序中的问题。
编辑:它会在高级组件中进行更新,因此在整个应用程序中都不是问题