const Room = (props) => {
const [hasError, setErrors] = useState(false);
const [rooms, setRooms] = useState([]);
return (
<div> <a onClick={() => deleteRoom()}</div>
)
}
const deleteRoom = () => {
//How to update setRooms here
}
如何使用setRooms
方法更新deleteRoom
?
答案 0 :(得分:3)
您只需在deleteRoom
组件内定义Room
函数。
const Room = (props) => {
const [hasError, setErrors] = useState(false);
const [rooms, setRooms] = useState([]);
const deleteRoom = () => {
setRooms(...);
}
return (
<div> <a onClick={() => deleteRoom()}</div>
)
}
答案 1 :(得分:2)
您可以将setRooms作为参数传递到deleteRoom中,然后在其中调用它。
例如
const Room = (props) => {
const [hasError, setErrors] = useState(false);
const [rooms, setRooms] = useState([]);
return (
<div> <a onClick={() => deleteRoom(setRooms)}</div>
)
}
const deleteRoom = (setRooms) => {
//How to update setRooms here
setRooms(...)
}
答案 2 :(得分:0)
您有2个选择:
deleteRoom
方法放入Room
组件中const Room = (props) => {
const [hasError, setErrors] = useState(false);
const [rooms, setRooms] = useState([]);
const deleteRoom = () => {
... use your state..
}
return (
<div> <a onClick={() => deleteRoom()}</div>
)
}
useState
函数作为参数传递给deleteRoom
。const Room = (props) => {
const [hasError, setErrors] = useState(false);
const [rooms, setRooms] = useState([]);
return (
<div> <a onClick={() => deleteRoom(setRooms, setErrors)}</div>
)
}
const deleteRoom = (setRooms, setErrors) => {
you can use setRooms() and setErrors() here
}