简体   繁体   中英

React Native useEffect - Warning: An effect function must not return anything besides a function, which is used for clean-up

I would like to implement a single function to check if a specific seat out 20 seats available is booked or not. If it is, it returns red and if it's not, a green is returned.

I implemented the fetch function as

const getSeatColor = (seat, vid) => {
    const [color, setColor] = useState(0);

    useEffect(async () => {
      const response = await fetch(
        `http://192.168.8.100/dbops/actions/seat_status.php?seat=${seat}&vid=${vid}`
      );
      const result = await response.json();
      setColor(result.message);
    }, []);

    return color;
  };

  const vid = 4;
  const seat1Color = getSeatColor(1, vid);
  const seat2Color = getSeatColor(2, vid);
  const seat3Color = getSeatColor(3, vid);
  const seat4Color = getSeatColor(4, vid);

return (
    <Screen style={{ paddingBottom: 5 }}>
         {/* Seat 1 starts */}
          <TouchableOpacity style={styles.seat} >
            <MaterialCommunityIcons name="seat" size={40} color={seat1Color} />
            <Button title="1" style={{ width: 20 }} color={seat1Color} />
          </TouchableOpacity>
          {/* Seat 1 ends */}
          {/* Seat 2 starts */}
          <TouchableOpacity style={styles.seat} >
            <MaterialCommunityIcons name="seat" size={40} color={seat2Color} />
            <Button title="2" style={{ width: 20 }} color={seat2Color} />
          </TouchableOpacity>
          {/* Seat 2 ends */}
          <View style={styles.seat}></View>
          <View style={styles.seat}></View>
          {/* Seat 3 starts */}
          <TouchableOpacity style={styles.seat} >
            <MaterialCommunityIcons name="seat" size={40} color={seat3Color} />
            <Button title="3" style={{ width: 20 }} color={seat3Color} />
          </TouchableOpacity>
     </Screen>
)

The green or red comes out ok but I get this Warning: An effect function must not return anything besides a function, which is used for clean-up.

I tried this How to call an async function inside a UseEffect() in React? but the same warning pops up. Please help or could there be another way to achieve this?

You're returning a promise using the async keyword directly as the callback argument. Try re-writing your useEffect like this:

 useEffect(() => { (async() => { const response = await fetch( `http://192.168.8.100/dbops/actions/seat_status.php?seat=${seat}&vid=${vid}` ); const result = await response.json(); setColor(result.message); })() }, []);

or wrap that code inside of another function and call it inside of useEffect

 const fetchData = async () => { const response = await fetch( `http://192.168.8.100/dbops/actions/seat_status.php?seat=${seat}&vid=${vid}` ); const result = await response.json(); setColor(result.message); } useEffect(() => { fetchData(); }, []);

useEffect not accept async function.

You can try make this:

const getSeatColor = (seat, vid) => {
    const [color, setColor] = useState(0);

    useEffect(() => {
      (async => {
         const response = await fetch(`http://192.168.8.100/dbops/actions/seat_status.php?seat=${seat}&vid=${vid}`);
         const result = await response.json();
         setColor(result.message);
      })();
    }, []);

    return color;
};

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