Reactjs react axios如何填充react大日历
如何使用API Axios中的数据填充大日历,创建阵列 徖Reactjs react axios如何填充react大日历,reactjs,axios,react-big-calendar,Reactjs,Axios,React Big Calendar,如何使用API Axios中的数据填充大日历,创建阵列 徖 class Calendar extends Component { constructor() { super(); this.state = { events: [], info: false, title: null, startDate: null, endDay: null, startTime: null, endTim
class Calendar extends Component {
constructor() {
super();
this.state = {
events: [],
info: false,
title: null,
startDate: null,
endDay: null,
startTime: null,
endTime: null,
description: null
};
this.toggle = this.toggle.bind(this);
this.toggleInfo = this.toggleInfo.bind(this);
}
handleChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
loadCalendar() {
axios
.get(api + "api/calendar", {
headers: {
"Content-type": "application/json",
Authorization: reqtoken
}
})
.then(json => {
json.data.data.data.map(data =>
this.setState(
events({
title: data.title,
start: data.startDate,
end: data.endDate
})
)
);
})
.catch(erros => {
console.log(erros);
});
}
componentDidMount() {
this.loadCalendar();
}
render() {
return (
<CardBody style={{ height: "40rem" }}>
<BigCalendar
className="d-sm-down-none"
style={{ height: "-webkit-fill-available" }}
{...this.props}
events={events}
views={["month", "week", "day"]}
step={30}
defaultDate={new Date(currYear, currMonth, 1)}
defaultView="month"
toolbar={true}
localizer={localizer}
/>
<BigCalendar
className="d-md-none"
{...this.props}
events={events}
views={["day"]}
step={30}
defaultDate={new Date(currYear, currMonth, 1)}
defaultView="day"
toolbar={true}
localizer={localizer}
/>
</CardBody>
)
使用Axios从API获取请求
class Calendar extends Component {
constructor() {
super();
this.state = {
events: [],
info: false,
title: null,
startDate: null,
endDay: null,
startTime: null,
endTime: null,
description: null
};
this.toggle = this.toggle.bind(this);
this.toggleInfo = this.toggleInfo.bind(this);
}
handleChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
loadCalendar() {
axios
.get(api + "api/calendar", {
headers: {
"Content-type": "application/json",
Authorization: reqtoken
}
})
.then(json => {
json.data.data.data.map(data =>
this.setState(
events({
title: data.title,
start: data.startDate,
end: data.endDate
})
)
);
})
.catch(erros => {
console.log(erros);
});
}
componentDidMount() {
this.loadCalendar();
}
render() {
return (
<CardBody style={{ height: "40rem" }}>
<BigCalendar
className="d-sm-down-none"
style={{ height: "-webkit-fill-available" }}
{...this.props}
events={events}
views={["month", "week", "day"]}
step={30}
defaultDate={new Date(currYear, currMonth, 1)}
defaultView="month"
toolbar={true}
localizer={localizer}
/>
<BigCalendar
className="d-md-none"
{...this.props}
events={events}
views={["day"]}
step={30}
defaultDate={new Date(currYear, currMonth, 1)}
defaultView="day"
toolbar={true}
localizer={localizer}
/>
</CardBody>
)
使用数据api重新编写html
class Calendar extends Component {
constructor() {
super();
this.state = {
events: [],
info: false,
title: null,
startDate: null,
endDay: null,
startTime: null,
endTime: null,
description: null
};
this.toggle = this.toggle.bind(this);
this.toggleInfo = this.toggleInfo.bind(this);
}
handleChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
loadCalendar() {
axios
.get(api + "api/calendar", {
headers: {
"Content-type": "application/json",
Authorization: reqtoken
}
})
.then(json => {
json.data.data.data.map(data =>
this.setState(
events({
title: data.title,
start: data.startDate,
end: data.endDate
})
)
);
})
.catch(erros => {
console.log(erros);
});
}
componentDidMount() {
this.loadCalendar();
}
render() {
return (
<CardBody style={{ height: "40rem" }}>
<BigCalendar
className="d-sm-down-none"
style={{ height: "-webkit-fill-available" }}
{...this.props}
events={events}
views={["month", "week", "day"]}
step={30}
defaultDate={new Date(currYear, currMonth, 1)}
defaultView="month"
toolbar={true}
localizer={localizer}
/>
<BigCalendar
className="d-md-none"
{...this.props}
events={events}
views={["day"]}
step={30}
defaultDate={new Date(currYear, currMonth, 1)}
defaultView="day"
toolbar={true}
localizer={localizer}
/>
</CardBody>
)
render(){
返回(
)
当您设置事件时,您正在覆盖以前的事件。如果您不正确地使用setState,您应该向该函数传递一个对象
相反,对于axios成功回调,您可以执行以下操作:
.then(json => {
const events = json.data.data.data.map(data =>
return {
title: data.title,
start: data.startDate,
end: data.endDate
}
);
this.setState({ events });
})
这假设您的事件数据实际上是在该嵌套对象中返回的json.data.data.data
(这看起来很奇怪,但事实就是这样)