The set*** function is called but the re-rendering of the function component is not executed.
P粉864872812
P粉864872812 2024-04-04 09:53:14
0
1
279

My script looks like below,

After the

api 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粉864872812
P粉864872812

reply all(1)
P粉517814372

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() 
    },[]);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!