[英]How to reset the value of a state in React Native?
I want to reset and save the state of the items inside initialState
to 0 on the click of the button resetvalues
.我想在单击
resetvalues
按钮时将initialState
中项目的 state 重置并保存为 0 。
const initialState = {
present_count: [0, 0, 0, 0, 0, 0, 0],
total_count: [0, 0, 0, 0, 0, 0, 0],
present: 0,
total: 0
};
export default class MarkAttendanceScreen extends Component {
constructor(props) {
super(props);
this.state = {
subjects: [],
text: "",
...initialState,
}
}
resetvalues = () => {
this.setState({ ...initialState });
};
...
};
NOTE: The current code is not changing and saving the state of those values.注意:当前代码不会更改并保存这些值的 state。 I used an alert box to check if the function is accessible, that is working fine.
我使用了一个警告框来检查 function 是否可以访问,这工作正常。 I am using AsyncStorage to save the modified state.
我正在使用 AsyncStorage 来保存修改后的状态。
Got it working!得到它的工作!
const defaultState = {
subjects: [],
text: "",
present_count: [0, 0, 0, 0, 0, 0, 0],
total_count: [0, 0, 0, 0, 0, 0, 0],
present: 0,
total: 0
}
export default class MarkAttendanceScreen extends Component {
constructor(props) {
super(props);
this.state = {
...defaultState,
refreshing: false,
}
}
resetvalues = () => {
this.setState({
...defaultState,
subjects: this.state.subjects,
text: this.state.text
});
};
...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.