I have a TextInput field that when a user is editing, they have the option to cancel. The goal is to revert the TextInput value back to the original value before the user started editing, however, even though the previous value is stored and the screen is re-rendering, the text on the screen stays however the user left it. I’m assuming I’m just misunderstanding how the TextInput component works.
This is the input in question:
<TextInput
style={styles.workoutheader}
editable={editableWorkout == workout.id}
onChangeText={(workoutName) => setWorkoutName(workoutName)}
>
{workout.name}
</TextInput>
and this is the cancel button:
<TouchableOpacity
style={styles.workoutheadericon}
onPress={() => {
console.log('Reverting to: ' + prevWorkoutName, prevWorkoutType, prevWorkoutWeight, prevWorkoutReps);
setWorkoutName(prevWorkoutName);
setWorkoutType(prevWorkoutType);
setWorkoutWeight(prevWorkoutWeight);
setWorkoutReps(prevWorkoutReps);
setEditableWorkout('');
setIsEditing(!isEditing);
setActionCounter(actionCounter + 1);
}}
>
{workout.name}
is coming from a firestore database, but I don’t think that’s related.
2
Answers
SOLUTION
I needed to target the workout being edited with
editableWorkout == workout.id ? workoutName : workout.name}
which let me differentiate between the workouts using Leon Si's answer.I don’t think the variable
workout
gets updated when you callsetWorkoutName
, try replacing{workout.name}
withworkoutName
or whatever variable name you used where you declared youruseState
function that created thesetWorkoutName
setter function, e.g.