react native list object code example
Example 1: how to use flatlist keyextractor
<FlatList
data={[{name: 'a'}, {name: 'b'}]}
renderItem={
({item}) => <Text>{item.name}</Text>
}
keyExtractor={(item, index) => index.toString()}
/>
Example 2: make a fixed list in react native
export default function App() {
const [enteredGoal,setEnteredGoal] = useState('');
const [courseGoals, setCourseGoals] = useState([]);
const goalInputHandler = (enteredText) => {
setEnteredGoal(enteredText);
}
const addGoalHandler = () => {
setCourseGoals(currentGoals =>
[...currentGoals,enteredGoal]
)
}
return (
<View style={styles.screen}>
<View>
<View style={styles.otherview}>
<TextInput
placeholder='A goal'
style={styles.textinput}
onChangeText={goalInputHandler}
value={enteredGoal}/>
<Button title='Add' onPress={addGoalHandler}/>
</View>
</View>
<ScrollView>
{courseGoals.map((goal) =>
<View key={goal} style={styles.listItem}>
<Text>{goal}</Text>
</View>)
}
</ScrollView>
</View>