reactjs - 调用时操作不更新减速器
问题描述
我无法让我的减速器更新。this.completeQuiz(id)
当我在调试器中触发但我的状态没有更新时,我可以进入操作。有任何想法吗?
import {
submitAnswer,
resetQuiz,
nextQuestion,
completeQuiz
} from "../actions/videos";
class TestYourselfScreen extends React.Component {
constructor(props) {
super(props);
this.onCapture = this.onCapture.bind(this);
}
completeQuiz = id => {
let video = this.props.videos.find(obj => obj.id == id);
let correctAnswers = video.results.correctAnswers;
const questionsFiltered = video.questions.filter(obj => obj.question != "");
completeQuiz({
id,
totalScore: correctAnswers.length / questionsFiltered.length
});
};
render() {
.....
return (
{questionsFiltered.length > 0 && !completed && (
<View
style={{
flex: 1
}}
>
....
<Button
title={lastQuestion ? "Finish" : "Next"}
buttonStyle={[styles.button]}
disabled={
!results.correctAnswers.includes(current) &&
!results.incorrectAnswers.includes(current)
? true
: false
}
onPress={() =>
lastQuestion ? this.completeQuiz(id) : this.next(id, current)
}
/>
</View>
)}
{completed === true && (
<View
style={{
flex: 1
}}
>
<ViewShot ref="viewShot" options={{ format: "jpg", quality: 0.9 }}>
...
</View>
)}
</ScrollView>
);
}
}
const mapStateToProps = state => {
return {
videos: state.tcApp.videos
};
};
const mapDispatchToProps = dispatch => ({
submitAnswer: data => dispatch(submitAnswer(data)),
resetQuiz: id => dispatch(resetQuiz(id)),
nextQuestion: data => dispatch(nextQuestion(data)),
completeQuiz: data => dispatch(completeQuiz(data))
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(TestYourselfScreen);
行动:
export const completeQuiz = data => ({
type: "COMPLETE",
data
});
减速器:
import { trimText } from "../helpers";
export function tcApp(
state = { videos: [], search: { videos: [], term: "" } },
action
) {
switch (action.type) {
....
case "COMPLETE": {
const { completed, totalScore, id } = action.data;
return {
videos: state.videos.map(video =>
video.id === id
? {
...video,
results: {
totalScore
},
completed: true
}
: video
),
search: { term: "", videos: [] }
};
}
default:
return state;
}
}
解决方案
我认为您的动作可以通过道具获得
completeQuiz = id => {
let video = this.props.videos.find(obj => obj.id == id);
let correctAnswers = video.results.correctAnswers;
const questionsFiltered = video.questions.filter(obj => obj.question != "");
this.props.completeQuiz({
id,
totalScore: correctAnswers.length / questionsFiltered.length
});
};
因为我们 mapDispatchToProps
希望能帮助到你
推荐阅读
- angular - 如何将 HighChart 添加到 Angular?
- python - 通过 OpenCV 应用分割掩码
- mysql - Clickhouse 按顺序时间和特定类型查找事件对
- lightningchart - 为散点图设置单个大小的大小
- javascript - 等待调用后,它不会执行下一行
- sql-server - SQL Server 服务偶尔会停止,并且 SQL Server 日志和事件日志都没有包含有关原因的线索
- vue.js - 无法在包 vue-qr 的 SSR nuxtjs 中显示 QR 图像
- javascript - Firebase 承诺未兑现 - MERN 应用
- python - 在powershell中调用python
- python - 如何避免函数中的文件打开方法