反应本机 |循环渲染 FlatList 的项目
react-native | Loop on rendering Items of FlatList
我会创建一个 FlatList
这个 Item
:
function Item({ marca, targa, index, allData, jwt }) {
const [modalVisible, setModalVisible] = useState(false);
const [encData, setEncData] = useState('');
console.log(jwt);
console.log(allData);
const content = {
data: allData
}
fetch('https://example.com/api/encrypt', {
method: 'POST',
body: JSON.stringify(content),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'authorization': jwt
}
})
.then(response => response.json())
.then(res => setEncData(res.message))
.catch(err => console.log(err));
return (
<React.Fragment>
<Modal
animationType='fade'
transparent={false}
visible={modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}
>
<View style={{flex:1, justifyContent: 'center', alignItems: 'center' }}>
<View style={styles.modalInsideView}>
<View style={{bottom: 50}}>
<Text style={[styles.buttonText, styles.medium]}>{marca} <Text style={styles.buttonText}>- {targa}</Text></Text>
</View>
<Text>{JSON.stringify(encData)}</Text>
<View style={{alignItems: 'center', justifyContent: 'center'}}>
<TouchableOpacity style={[styles.button, {backgroundColor: '#00b0ff'}]} onPress={ () => setModalVisible(!modalVisible) }>
<Ionicons
name={'ios-close'}
size={40}
style={{ color: 'white' }}
/>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
<TouchableOpacity
style={[styles.infoVehicle, {marginTop: index === 1 ? 10 : 18}]}
onPress={ () => setModalVisible(true) }>
<View style={{flexDirection:'row', alignItems: 'stretch', alignItems: 'center', justifyContent: 'space-between'}}>
<View style={{}}>
<Text style={[styles.buttonText, styles.medium]}>{marca} <Text style={styles.buttonText}>- {targa}</Text></Text>
</View>
<View style={{}}>
<Image
style={{width: 40, height: 40, opacity: 0.5}}
source={require('../../images/qr-example.png')}
/>
</View>
</View>
</TouchableOpacity>
</React.Fragment>
);
}
但是,我发现元素<Text>{JSON.stringify(encData)}</Text>
的内容一直在变化,就好像Item
函数在循环一样。为什么?
在此 link 您可以找到我为该页面编写的所有代码。
我可以看到你将这部分放在渲染函数中:
fetch('https://example.com/api/encrypt', {
method: 'POST',
body: JSON.stringify(content),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'authorization': jwt
}
})
.then(response => response.json())
.then(res => setEncData(res.message))
.catch(err => console.log(err));
React 可能会多次调用函数 Item() 进行渲染,每次都会引入一个新的 API 调用,并在 api 调用时依次调用 setEncData
成功。这就引入了一个状态变化,React 会再次调用 Item() 重新渲染,然后引入一个循环。要解决此问题,您可以像这样将 fetch() 放在 useEffect
中:
useEffect(() => {
const content = {
data: allData
}
fetch('https://example.com/api/encrypt', {
method: 'POST',
body: JSON.stringify(content),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'authorization': jwt
}
})
.then(response => response.json())
.then(res => setEncData(res.message))
.catch(err => console.log(err));
}, []) // make this an empty array
更新:allData
可能是一个对象,不会通过浅层相等检查
我会创建一个 FlatList
这个 Item
:
function Item({ marca, targa, index, allData, jwt }) {
const [modalVisible, setModalVisible] = useState(false);
const [encData, setEncData] = useState('');
console.log(jwt);
console.log(allData);
const content = {
data: allData
}
fetch('https://example.com/api/encrypt', {
method: 'POST',
body: JSON.stringify(content),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'authorization': jwt
}
})
.then(response => response.json())
.then(res => setEncData(res.message))
.catch(err => console.log(err));
return (
<React.Fragment>
<Modal
animationType='fade'
transparent={false}
visible={modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
}}
>
<View style={{flex:1, justifyContent: 'center', alignItems: 'center' }}>
<View style={styles.modalInsideView}>
<View style={{bottom: 50}}>
<Text style={[styles.buttonText, styles.medium]}>{marca} <Text style={styles.buttonText}>- {targa}</Text></Text>
</View>
<Text>{JSON.stringify(encData)}</Text>
<View style={{alignItems: 'center', justifyContent: 'center'}}>
<TouchableOpacity style={[styles.button, {backgroundColor: '#00b0ff'}]} onPress={ () => setModalVisible(!modalVisible) }>
<Ionicons
name={'ios-close'}
size={40}
style={{ color: 'white' }}
/>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
<TouchableOpacity
style={[styles.infoVehicle, {marginTop: index === 1 ? 10 : 18}]}
onPress={ () => setModalVisible(true) }>
<View style={{flexDirection:'row', alignItems: 'stretch', alignItems: 'center', justifyContent: 'space-between'}}>
<View style={{}}>
<Text style={[styles.buttonText, styles.medium]}>{marca} <Text style={styles.buttonText}>- {targa}</Text></Text>
</View>
<View style={{}}>
<Image
style={{width: 40, height: 40, opacity: 0.5}}
source={require('../../images/qr-example.png')}
/>
</View>
</View>
</TouchableOpacity>
</React.Fragment>
);
}
但是,我发现元素<Text>{JSON.stringify(encData)}</Text>
的内容一直在变化,就好像Item
函数在循环一样。为什么?
在此 link 您可以找到我为该页面编写的所有代码。
我可以看到你将这部分放在渲染函数中:
fetch('https://example.com/api/encrypt', {
method: 'POST',
body: JSON.stringify(content),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'authorization': jwt
}
})
.then(response => response.json())
.then(res => setEncData(res.message))
.catch(err => console.log(err));
React 可能会多次调用函数 Item() 进行渲染,每次都会引入一个新的 API 调用,并在 api 调用时依次调用 setEncData
成功。这就引入了一个状态变化,React 会再次调用 Item() 重新渲染,然后引入一个循环。要解决此问题,您可以像这样将 fetch() 放在 useEffect
中:
useEffect(() => {
const content = {
data: allData
}
fetch('https://example.com/api/encrypt', {
method: 'POST',
body: JSON.stringify(content),
headers: {
'Content-Type': 'application/json; charset=utf-8',
'authorization': jwt
}
})
.then(response => response.json())
.then(res => setEncData(res.message))
.catch(err => console.log(err));
}, []) // make this an empty array
更新:allData
可能是一个对象,不会通过浅层相等检查