React Native component shows only one complaint instead of other more features
P粉925239921
2023-08-16 09:12:20
<p>I'm working on a screen that gets route parameters from the previous screen for navigation but it only shows the first complaint and not any other added complaints, I don't know what is causing this issue , the following is the code: </p>
<pre class="brush:php;toolbar:false;">const ECScreen = ({ route, navigation }) => {
const { imageUri, complaintDate, complaintId } = route.params;
const [complaints, setComplaints] = useState([]);
useEffect(() => {
if (imageUri && complaintDate) {
const newComplaint = {
id: complaints.length 1, // Assign next available ID
imageUri: imageUri,
date: complaintDate,
status: 'Pending',
};
setComplaints([...complaints, newComplaint]);
}
}, []);</pre>
You call this function with an empty dependency array on useEffect, which is called when the component is mounted, and the initial complaint is also empty.
Therefore,
[...complaints, newComplaint]
will be equal to one value, which is yournewComplaint
.Please tell me which variable you wish to update
complaints
?