Home  >  Q&A  >  body text

React Native component shows only one complaint instead of other more features

<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>
P粉925239921P粉925239921453 days ago509

reply all(1)I'll reply

  • P粉937382230

    P粉9373822302023-08-17 00:44:57

    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 your newComplaint.
    Please tell me which variable you wish to update complaints?

    reply
    0
  • Cancelreply