我想将其更改为挂钩格式或函数。
代码
updateRoom(roomId){
if (roomId === '') {
this.setState({RoomId: '', Room: ''})
} else {
this.setState({RoomId: roomId.key, Room: roomId.name});
}
}
我的三元操作员缺少什么?
我是否必须将功能名称更改为useUpdateRoom
来表示钩函数?
钩
function UpdateRoom(roomId) {
return '' ? setRoomId : Room
}
function App() {
const [rooms, setRooms] = useState('');
const [roomId, setRoomId] = useState('');
const [user, setUser] = useState('');
return (
<div className="app">
<div className="room">
{rooms.map((room, index) => (
<Room key={index} index={index} room={room} />
))}
</div>
</div>
)
}
您的三元不评估RoomID。为了清楚起见,我还将偏移重命名为" ID"。应该是:
function App() {
const [rooms, setRooms] = useState('');
const [roomId, setRoomId] = useState('');
const [user, setUser] = useState('');
function updateRoom(id) {
return id === ''
? setRoomId('')
: setRoomId(id)
}
return (
<div className="app">
<div className="room">
{rooms.map((room, index) => (
<Room key={index} index={index} room={room} />
))}
</div>
</div>
)
}
另外,您需要将功能放入应用程序中,以便它可以访问SetRoomID。
最后,我看不到您在哪里打电话,但这将是:
<button onClick={() => updateRoom(some_id)}>Change Room</button>