i tried to build a TODO app with React Native
const [ enteredGoal, setEnteredGoal ] = useState("");
const [ courseGoals, setCourseGoals ] = useState([]);
const goalInputHandler = (enteredText) => {
setEnteredGoal(enteredText);
};
const addGoalHandler = () => {
let arrGoals = courseGoals;
arrGoals.push(enteredGoal);
setCourseGoals(arrGoals);
};
return (
<View style={styles.screen}>
<View style={styles.inputContainer}>
<TextInput style={styles.input} onChangeText={goalInputHandler}/>
<Button title="ADD" onPress={addGoalHandler}/>
</View>
<View>
{
courseGoals.map((goal) => {
<View key={goal} style={styles.listItem}><Text>{goal}</Text></View>
})
}
</View>
</View>
);
the idea is to list the setCourseGoals array, but apparently after assigning a new text, it doesn't list anything. Its not until the text changes that the list is re-rendered. Any idea on why this happens?? the "useState" function is asynchronous, which assigns the value after rendering? Thanks in advance.