[英]How to reset the value of a state in React Native?
我想在單擊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 });
};
...
};
注意:當前代碼不會更改並保存這些值的 state。 我使用了一個警告框來檢查 function 是否可以訪問,這工作正常。 我正在使用 AsyncStorage 來保存修改后的狀態。
得到它的工作!
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.