【发布时间】:2018-12-18 16:16:18
【问题描述】:
我是react-redux 的新手。在这里,我有一个减速器,就像,
const initialState = {
Low: [
{
id: 0,
technologyId: 0,
technology: '',
type: '',
count: '',
allowded: 6,
level: 'EASY'
}
],
Medium: [
{
id: 0,
technologyId: 0,
technology: '',
type: '',
count: '',
allowded: 7,
level: 'MEDIUM'
}
],
High: [
{
id: 0,
technologyId: 0,
technology: '',
type: '',
count: '',
allowded: 7,
level: 'TOUGH'
}
]
}
export default function QuizData(state = initialState, action) {
switch (action.type) {
case QUIZ_DATA:
return {
...state,
[action.data.type]: [...action.data.tobeData],
error: false,
}
case ADD_NEW:
return {
...state,
[action.data.addtype]: action.data.addData,
error: false,
}
case REMOVE_TECH:
return {
...state,
[action.data.removeType]: action.data.newArr,
error: false,
}
case RESET_QUIZ:
return {
...initialState,
error: false,
}
}
现在,点击按钮,我正在调用一个将数据重置为初始状态的操作。
this.props.resetQuiz();
这是
export function resetQuiz() {
return {
type: RESET_QUIZ
}
}
我在哪里使用它
让 newData = { ...this.props.data };同时在组件中使用它来做一些操作。
现在发生的情况是在执行一些操作后,初始状态数据会随着一些新值发生变化,..
但是,在单击按钮时,我想将其设置为 initialState。
所以,当我尝试那个时候,initialState 也得到了相同的值。所以,它没有重置。
我在组件中使用它,
data: state.QuizData // In statetoprops.
let criterias = [{
type: 'Low',
noc: 6,
id: 1,
data: this.props.data["Low"]
}, {
type: 'Medium',
noc: 7,
id: 2,
data: this.props.data["Medium"]
},
{
type: 'High',
noc: 7,
id: 3,
data: this.props.data["High"]
}]
在像这样的组件中使用动作时,
createQuestionBankQuiz = () => {
this.props.resetQuiz();
history.push({
pathname: "/quiz-setup/" + `${this.props.jdId}`
});
};
export default connect(mapStateToProps, { fetchListOfQuiz, updateQuestionViewMode, enableJob, resetQuiz })(LandingScreen);
我更新的方式是
onChange(event, tobeupdated, id, type, noc, data) {
let newData = { ...this.props.data };
let errorState;
let isDuplicate;
let addedRow;
if (newData) {
let data = newData[type].map((object, index) => {
if (object.id === id) {
object[tobeupdated] = event.target.value;
const tobeData = newData[type];
this.props.updateLowLevel({ tobeData, type }).then(() => {
let criteria_filled = this.disableAddbutton({ ...this.props.data }, type);
addedRow = `new${type}RowAdded`;
this.setState({
[addedRow]: criteria_filled ? true : false
})
const tobechecked = newData[type].filter(item => item.id === id);
isDuplicate = this.checkPreviousSelected(newData, type, tobechecked[0].technology, tobechecked[0].type);
if (isDuplicate) {
toastr.error("Duplicate criteria. Please change it.");
object["technology"] = '';
object["type"] = '';
const tobeData = newData[type];
this.props.updateLowLevel({ tobeData, type });
}
});
}
});
errorState = `show${type}Error`;
if (tobeupdated !== "count") {
this.getSelectionQuestionsNumber(type, id, noc);
}
let validateData = this.validate(type, noc);
this.setState({
[errorState]: validateData
})
}
}
我做错了什么?
【问题讨论】:
-
如果您想重置为
return initialState,为什么不能这样做? IE。case RESET_QUIZ: return initialState; -
我也试过了。
-
显然您正在将测验数据设置在其他地方。如果您在减速器中返回
initialState,则您的状态不会重置为initialState。你是如何调度动作的? -
你的代码返回一个新的状态而不是重置
initialState。所以试试Object.assign(initialState,{a:somevalue})这样的代码。 -
@lx1412 这违背了
react-redux的原则。
标签: javascript reactjs redux react-redux