我正在使用React JS开发一个Web应用程序。我是React JS的新手。我现在正在做的是从发出api请求的远程服务器获取数据,并将其显示在页面上。
但是问题是当我调整浏览器大小时,总是会调用React组件的mount事件。我正在componentDidMount事件中进行API调用。因此,每当我调整浏览器大小时,它都会对服务器进行额外的调用并刷新数据。那不应该发生。因此,为确保API调用仅进行一次,我检查了像这样在componentDidMount事件中使用的状态。
componentDidMount()
{
if(!this.state.initialized)
{
//Api call
this.fetchEvents();
}
this.setState({
initialized: true
})
}
上面的代码仍在向服务器发出api请求,并在调整大小的浏览器上刷新数据。为什么会这样呢?怎么预防呢?我想了解它的内部原理,而不是立即解决它的方法。
这是我的整个EventListComponent,以防万一人们想看代码
class EventListComponent extends Component {
constructor(props) {
super(props);
this.redirectToCreatePage = this.redirectToCreatePage.bind(this);
this.redirectToEditPage = this.redirectToEditPage.bind(this);
this.showDeleteEventConfirmation = this.showDeleteEventConfirmation.bind(this);
this.closeDeleteConfirmation = this.closeDeleteConfirmation.bind(this);
this.deleteEvent = this.deleteEvent.bind(this);
this.state = {
initialized :false
}
}
componentDidMount()
{
if(!this.state.initialized)
{
this.fetchEvents();
}
this.setState({
initialized: true
})
}
fetchEvents()
{
this.props.startFetchingEvents();
getHttpClient().get('event/list', { })
.then((response) => {
this.props.completeFetchingEvent(response.data);
})
.catch((error) => {
this.props.errorFetchingEvents();
});
}
redirectToEditPage(event)
{
// return alert(event.id)
this.props.setEditingEvent(event);
this.props.history.push({
pathname : '/event/'+ event.id +'/edit'
});
}
redirectToAlbum(event)
{
this.props.history.push("/event/" + event.code + "/album");
}
redirectToCreatePage()
{
this.props.history.push('event/create');
}
redirectToUploadPage(event)
{
this.props.history.push('event/' + event.id + '/file/upload')
}
redirectToMatchPhotosPage(event)
{
this.props.history.push('/event/' + event.id + '/match/photos');
}
showDeleteEventConfirmation(event)
{
this.props.promptDeleteConfirmation(event);
}
closeDeleteConfirmation()
{
this.props.closeDeleteConfirmation();
}
deleteEvent()
{
this.props.startDeletingEvent(this.props.eventToBeDeleted);
let body = { id : this.props.eventToBeDeleted.id }
let path = "event/delete";
getHttpClient()
.post(path, body)
.then((response) => {
this.props.completeDeletingEvent();
window.location.reload()
})
.catch((error) => {
this.props.errorDeletingEvent();
});
}
render() {
let eventWidgets = this.props.events.map((item, index)=> {
return <Grid key={index} item sm={4} xs={4}>
<EventWidget
showDeleteEventConfirmation={this.showDeleteEventConfirmation.bind(this)}
redirectToUploadPage={this.redirectToUploadPage.bind(this)}
redirectToAlbum={this.redirectToAlbum.bind(this)}
redirectToEditPage={this.redirectToEditPage.bind(this)}
redirectToMatchPhotosPage={this.redirectToMatchPhotosPage.bind(this)}
{...item} />
</Grid>
})
return (
<div className={scss['page-container']}>
<Button onClick={this.redirectToCreatePage} color="secondary" type="button" variant="raised">Post new event</Button>
<BlockUi tag="div" blocking={this.props.requestingEvents}>
<Grid
style={{ marginTop: "10px", minHeight: "100px" }}
spacing={16}
container
justify="flex-start"
>
{eventWidgets}
</Grid>
</BlockUi>
<Dialog
open={this.props.showDeleteConfirmationDialog}
onClose={this.closeDeleteConfirmation}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<BlockUi tag="div" blocking={this.props.deletingEvent}>
<DialogTitle id="alert-dialog-title">{"Confirmation"}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Are you sure to delete the event? You cannot undo the action.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={this.closeDeleteConfirmation} color="primary">
No
</Button>
<Button onClick={this.deleteEvent} color="primary" autoFocus>
Yes
</Button>
</DialogActions>
</BlockUi>
</Dialog>
</div>
);
}
}
function mapStateToProps(state)
{
return {
events: state.eventList.events,
requestingEvents : state.eventList.requestingList,
showDeleteConfirmationDialog : state.eventList.showDeleteConfirmationDialog,
deletingEvent : state.eventList.deletingEvent,
eventToBeDeleted : state.eventList.eventToBeDeleted
};
}
function matchDispatchToProps(dispatch)
{
return bindActionCreators({
startFetchingEvents: EventListActions.startFetchingEvents,
completeFetchingEvent: EventListActions.completeFetchingEvents,
errorFetchingEvents : EventListActions.errorFetchingEvents,
setEditingEvent: EditEventActions.setEditingEvent,
promptDeleteConfirmation : EventListActions.promptDeleteConfirmation ,
closeDeleteConfirmation : EventListActions.closeDeleteConfirmation,
startDeletingEvent : EventListActions.startDeletingEvent,
completeDeletingEvent : EventListActions.completeDeletingEvent,
errorDeletingEvent : EventListActions.errorDeletingEvent
}, dispatch);
}
EventListComponent.propTypes = {
classes: PropTypes.shape({
headerTheme: PropTypes.string
}).isRequired
};
const enhance = compose(withWidth(), withStyles(themeStyles, { withTheme: true }), connect(mapStateToProps, matchDispatchToProps))
export default enhance(EventListComponent);