【发布时间】:2020-09-18 12:17:31
【问题描述】:
我对@987654321@ + Typescript 很陌生,我正在尝试找出为什么会出现以下错误。
React 16.9
const STEP_STATUS = {
COMPLETED: "Completed",
INCOMPLETE: "Incomplete",
};
const Foo = () => {
const [state, setState] = useState({
step: 1,
firstName: '',
lastName: '',
status: STEP_STATUS["INCOMPLETE"],
});
const nextStep = () => {
setState({...state, step: state.step + 1});
}
useEffect(() => {
if (state.step + 1 !== state.step) {
// setState(prev => {...prev, state.step = STEP_STATUS["COMPLETED"]}); // I tried this too
setState(...state, state.status = STEP_STATUS["COMPLETED"]);
} else {
console.log("error"); // don't mind this line
}
}, [state]);
我正在尝试使用useEffect 创建一个副作用,在按下按钮nextStep(将用户带到下一页)后,我想更新当前页面的状态在我们进入下一页之前“完成”。
我可能真的做错了。
任何帮助都会非常有帮助。
当前错误
×
TypeError: state is not iterable
53 | useEffect(() => {
54 | if (state.step + 1 !== state.step) {
55 | // setState(prev => {...prev, state.step = STEP_STATUS["COMPLETED"]});
> 56 | setState(...state, state.status = STEP_STATUS["COMPLETED"]);
| ^ 57 | } else {
58 | console.log("error");
59 | }
【问题讨论】:
标签: javascript reactjs typescript react-native react-redux