【发布时间】:2021-06-23 23:26:28
【问题描述】:
我正在编写 React Native 教程。在下面的代码中,courseGoals 是使用 useState 函数在状态中声明的,但是当在 addGoalHandler 函数中调用 setCourseGoals 函数时,currentGoals 已作为参数传递,而不是 courseGoals。为什么是这样?是不是因为如果我们在 setCourseGoals 中使用一个函数,参数会自动总是得到 courseGoals 的值?
export default function App() {
const [enteredGoal, setEnteredGoal] = useState('');
const [courseGoals, setCourseGoals] = useState([]);
const goalInputHandler = (enteredText) => {
setEnteredGoal(enteredText);
};
const addGoalHandler = () => {
setCourseGoals(currentGoals => [...currentGoals, enteredGoal]);
};
【问题讨论】:
标签: javascript reactjs react-native use-state