Home  >  Q&A  >  body text

The set*** function is called but the re-rendering of the function component is not executed.

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粉864872812P粉864872812223 days ago370

reply all(1)I'll reply

  • P粉517814372

    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() 
        },[]);

    reply
    0
  • Cancelreply