【发布时间】:2021-06-11 18:47:00
【问题描述】:
经过大量搜索后,我一直在寻找如何使用类而不是函数将数据从 react 发送到 django api 那么我如何将其转换为功能组件(这不是我的代码,只是一个示例)
export default class CreateRoomPage extends Component {
constructor(props) {
super(props);
this.state = {
guestCanPause: this.props.guestCanPause,
votesToSkip: this.props.votesToSkip,
errorMsg: "",
successMsg: "",
};
this.handleRoomButtonPressed = this.handleRoomButtonPressed.bind(this);
this.handleVotesChange = this.handleVotesChange.bind(this);
}
handleGuestCanPauseChange(e) {
this.setState({
guestCanPause: e.target.value === "true" ? true : false,
});
}
handleRoomButtonPressed() {
const requestOptions = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
votes_to_skip: this.state.votesToSkip,
guest_can_pause: this.state.guestCanPause,
}),
};
fetch("/api/create-room", requestOptions)
.then((response) => response.json())
.then((data) => this.props.history.push("/room/" + data.code));
}
renderCreateButtons() {
return (
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Button
color="primary"
variant="contained"
onClick={this.handleRoomButtonPressed}
>
Create A Room
</Button>
</Grid>
</Grid>
);
}
}
【问题讨论】:
标签: reactjs django django-rest-framework react-hooks