從 API 獲取數(shù)據(jù)后,我將其設(shè)置為狀態(tài),并在 Flatlist 中呈現(xiàn)項(xiàng)目,當(dāng)我從中選擇任何項(xiàng)目時(shí),我會操作數(shù)據(jù)并向名為“toggle: true”的項(xiàng)目對象添加一個(gè)新屬性當(dāng)我從列表中選擇任何項(xiàng)目時(shí)效果很好,我添加了一個(gè)基于的邊框toggle,但是當(dāng)我返回上一個(gè)屏幕然后重新打開列表屏幕時(shí),我可以看到在項(xiàng)目周圍呈現(xiàn)的邊框,盡管我在卸載時(shí)重置了狀態(tài)屏幕那么我在這里犯了什么錯(cuò)誤?代碼片段數(shù)據(jù)export default { ... services: [ { id: 0, name: 'nameS0', logo: 'https://cdn2.iconfinder.com/data/icons/hotel-98/64/hair-dryer-tools-beauty-hairdressing-512.png', price: 19.99, }, ], employees: [ { id: 0, name: 'name0', img: 'https://www.visualelementmedia.com/wp-content/uploads/2015/04/person-4-400x629.jpg', }, ... ],};const VendorProfile = ({navigation}) => { const [services, setServices] = React.useState(null); const [employees, setEmployees] = React.useState(null); const [serviceSelected, setServiceSelected] = React.useState(null); const [employeeSelected, setEmployeeSelected] = React.useState(null); // For selected Item (services, employees) const itemSelected = (data, id) => { const updated = data.map((item) => { item.toggle = false; if (item.id === id) { item.toggle = true; data === services ? setServiceSelected(item) : setEmployeeSelected(item); } return item; }); data === services ? setServices(updated) : setEmployees(updated); }; ... const renderEmployees = ({item}) => { return ( <TouchableOpacity onPress={() => itemSelected(employees, item.id)} delayPressIn={0} style={styles.employeeContainer}> <EmployeePattern style={{alignSelf: 'center'}} /> <View style={styles.employeeLogo}> <Image source={{uri: item.img}} style={[styles.imgStyle, {borderRadius: 25}]} /> ); };
卸載屏幕后重置狀態(tài) - 掛鉤?
青春有我
2022-12-29 15:15:51