简体   繁体   中英

React functional component doesn't re-render when prop changes

I'm using axios to request in my useEffect and the prop I pass is only a simple id. Here's the main part of my code;

    return (
        <>
            <Table striped bordered hover>

You forgot to pass the prop stationData in the dependency array:

    const [stationData, setStationData] = useState([]);

    useEffect(() => {
        console.log(`Fetching station ${stationId}`)
        axios.get(`http://localhost:3001/stations/${stationId}`)
            .then(response => {
                console.log(response);
                setStationData(response.data);
            })
            .catch(error => {
                console.log(error);
            });
    }, [stationData]); <--- HERE

    return (
        <>
            <Table striped bordered hover>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM