nad34
nad34

Reputation: 393

How can I parse the response of axios to an attribute in a React component

So, I have an axios call to an api which I am able to console out however I need to parse the result into an attribute of a React component but all I get is that it is undefined

my code is as follows and I am sorry for the dodgy code I am just trying to get it to work... somehow

useEffect(() => {
    loadTableData();
  }, []);


  const loadTableData = () => {
   
    axios
      .get(`https://jsonplaceholder.typicode.com/todos`)
      .then(res => {
        const { data } = res;
      
        settableData({ ...tableData, data });
  
      })
      .catch(err => {
        
        if (err.response.status === 401) {
          toast.error(`You do not have permission to view this resource`);
        }
          if (err.response.status === 404) {
          toast.error(`The resource cannot be found`);
        }

      });
     
    };

 <Table data={data} />

I get an error saying data is not defined even when I try to output only one element like {data[0].title}

how can I get hold of data and send it to the data= attribute of <Table

If there is a better way than how I planed to do it, I appreciate all suggestions

Thanks in advance

Upvotes: 0

Views: 91

Answers (1)

user9760669
user9760669

Reputation:

Try the simplest solution

{data && <Table data={data} />}

Upvotes: 1

Related Questions