Reputation: 215
I am working on a React Native tutorial. In the code below, courseGoals was declared in the state using the useState function, but when calling the setCourseGoals function within the addGoalHandler function, currentGoals has been passed as the parameter and not courseGoals. Why is this? Is it because if we are using a function within setCourseGoals, the parameter will automatically always get the courseGoals value?
export default function App() {
const [enteredGoal, setEnteredGoal] = useState('');
const [courseGoals, setCourseGoals] = useState([]);
const goalInputHandler = (enteredText) => {
setEnteredGoal(enteredText);
};
const addGoalHandler = () => {
setCourseGoals(currentGoals => [...currentGoals, enteredGoal]);
};
Upvotes: 1
Views: 96
Reputation: 2723
Yes.
And [...currentGoals, enteredGoal]
in the code is appending the courseGoals
array with new goal.
You can rename the parameter as you want. And it will still work. E.g:
setCourseGoals(courceGoals => [...courceGoals, enteredGoal]);
Upvotes: 1