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?