My script looks like below,
After theapi gets the item, I want to re-render the component and make EnhancedTable
appear.
But with this script, api fetch is successful, but EnhancedTable does not appear.
response.data returns the correct data, but no re-rendering is performed.
Where did I go wrong?
const MetaPanel = (props) =>{ const [drawingItems,setDrawingItems] = React.useState([]); useEffect(() => { console.log("File List"); var formData = new FormData(); axios.defaults.xsrfHeaderName = "X-CSRFTOKEN"; axios.defaults.xsrfCookieName = "csrftoken"; axios.defaults.withCredentials = true; axios.get(`/api/drawings/`) .then(function (response) { console.log(response.data); setDrawingItems(response.data); }) .catch(function (response) { console.log(response); }); },[]); return ( <div> <div> <p>FileList</p> {drawingItems.length != 0 & <EnhancedTable data={drawingItems}></EnhancedTable> } </div> </div> ); }
P粉5178143722024-04-05 09:14:59
Can you try it:
FileList
{drawingItems &&}
By the way, you can improve the useEffect part
useEffect(() => { const fetchData= async()=>{ const response = await axios.get(`/api/drawings/`) if(response){ setDrawingItems(response?.data); } } fetchData() },[]);