【发布时间】:2021-09-13 11:16:22
【问题描述】:
我想在按下 TouchableOpacity 时提醒来自 API 的消息。我有一个处理seatBooking 的函数和为消息调用seatBooking 函数的单个函数。 这是我的代码
const generalBooking = (seat, uid, vid, tc_id) => {
const [msg, setMsg] = useState(0);
useEffect(() => {
(async () => {
const response = await fetch(
`http://192.168.8.100/dbops/actions/make_booking.php?seat=${seat}&uid=${uid}&vid=${vid}&tc_id=${tc_id}`
);
const result = await response.json();
setMsg(result.message);
// alert(result.message);
})();
}, []);
return msg;
};
function seat1Booking(seat1, uid, vid, tc_id) {
const msg = generalBooking(1, uid, vid, tc_id);
Alert.alert(msg);
}
return (
<Screen style={{ paddingBottom: 5 }}>
{/* Seat 1 starts */}
<TouchableOpacity style={styles.seat} onPress={seat1Booking}>
<MaterialCommunityIcons name="seat" size={40} color={seat1Color} />
<Button title="1" style={{ width: 20 }} color={seat1Color} />
</TouchableOpacity>
{/* Seat 1 ends */}
</Screen>
)
当我在console.log(seat1booking()) 外面登录时,generalBooking 和个人 seat1Booking 功能正常,但是当我将它传递给 TouchableOpacity 时,它会发出警告
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
....
我尝试阅读了许多链接Error Invalid hook call. Hooks can only be called inside of the body of a function component、How to fix React(TypeScrtipt) error "Invalid hook call."?,包括错误消息中提供的链接都是徒劳的。
问题可能是调用处理座位预订的单个函数的函数,例如,seat1Booking 还是 generalBooking 函数?请指教
【问题讨论】:
标签: react-native react-hooks use-effect