I have some problems about array in my project but don't know how to fix
My array look like :
const [array, setArray] = useState([
{ id: 1, name: 'Luca', classes: [{ id: 1, className: '', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
{ id: 2, name: 'Jin', classes: [{ id: 1, className: '', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
])
< FlatList
data = { array }
renderItem = {({ item, index }) => {
return (
<View>
<FlatList
data={item.classes}
renderItem={({ item, index }) => {
return (
<View >
<TextInput
placeholder={''}
onChangeText={(text) => onChangeClassName(item, text)}
value={item.className}
/>
</View>
)
}}
></FlatList>
</View>
)
}}
></FlatList >
onChangeClassName
onChangeClassName = (item, text) => {
const newArr = arr
for (let i = 0; i < newArr.length; i++) {
for (let z = 0; z < newArr[i].classes.length; z++) {
if (newArr[i].classes[z] === item ) {
newArr[i].classes[z].className = text
} }]
}
}
}
setArray([...arr])
}
So each student has name and classes, classes of each student is different and i change it by using textInput. My problem is when i set className for one of students , both className in same position of students changed
ex: Clicked in classes[0] of Luca -> set text = '7A'
array will changed like this.
const [array, setArray] = useState([
{ id: 1, name: 'Luca', classes: [{ id: 1, className: '7A', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
{ id: 2, name: 'Jin', classes: [{ id: 1, className: '7A', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
])
Any one have solution ? Pls help me

