react-hooksrendering

React Native- Hooks issue


  // Fetch Leads
const getLeadnew = useCallback(() => {
  //fetch('https://3ed6-122-165-218-218.in.ngrok.io/api/Proaddall', {
  fetch(`${BASE_URL}/api/leadsappall`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      Authorization: `Bearer ${userInfo.token}`,
    },
  })
    .then((res) => {
      return res.json();
    })
    .then((res) => {
      console.log(res.leads);
      setLeadnew(res.leads);
      //Filter Method input
      //console.log(Leadnew);
    })
    .catch((e) => {
      console.log(e);
    });
},);

#How to fix multiple re-renders This can be using the flatlist to view and apply the filter method. Each filter method the data can be re-render the multiple times to get the "cannot convert undefined or null to object". And also using same method to get name to match the id.


Solution

  • Could you show some example of the code for future question.

    If you want to use the useEffect hook to call some api and want only render once you could try add the dependency array to and empty array something like this:

    React.useEffect(() => {
       myApiFunction().then(_ => console.log('myApiFunction'));
      }, []);
    

    myApiFunction will be the function that have your code to fetch the api with some try catch to handle errors.

    I hope this help. cheers