У меня есть состояние в родительском компоненте, в котором есть массив пациентов. My Child Component 4
- это Countdown component
(// компонент обратного отсчета визуализирует индивидуальный обратный отсчет для каждого пациента в массиве), и когда обратный отсчет дойдет до ‹= 0, я хотел бы сбросить значение locationInfo & status
пациента, чтобы оно было пустой строкой в массиве пациентов. В моем родительском компоненте у меня есть функция resetPatient
, которая отображает patient array
и должна устанавливать значения(locationInfo & status)
пациента, у которого счетчик ‹= 0, на пустые строки. Функция resetPatient передается как подпорка моему компоненту обратного отсчета (4). В компоненте обратного отсчета я вызываю функцию this.props.resetPatient
, когда счетчик достигает ‹= 0. Однако у меня это не работает. Состояние родительского компонента не меняется.
Parent Component
- Child Component 1
- Child Component 2
- Child Component 3
- Child Component 4
Parent component
export default class ObservationWorkflow extends React.Component {
constructor(props) {
super(props);
this.state = {
patients = [
{ room: "1", name: 'John', locationInfo: 'TV', status: 'Awake'},
{ room: "2", name: 'Shawn, locationInfo: 'Toilet', status: 'Awake'},
{ room: "3", name: 'Gereth, locationInfo: 'Garden', status: 'Awake'}
]
}
this.resetPatient = this.resetPatient.bind(this);
}
resetPatient = (patient, index) => {
this.setState(prevState => ({
patients: prevState.patients.map((patient, i) => {
if (i === index) {
return {
...patient,
locationInfo: '',
status: ''
};
}
return patient;
}),
}));
}
render() {
return (
<Child component(1)={this.resetPatient} // then child component 2 passes it down as props to 3 then 4.
)
}
}
Countdown component(4)// countdown component renders individually countdown for each patient in the array.
export default class ObservationCountDown extends React.Component {
constructor(props) {
super(props);
this.state = {
obsTimeleft: undefined
};
this.countDown = this.countDown.bind(this);
this.startCountdown = this.startCountdown.bind(this);
this.countDownInterval = null;
}
countDown() {
const { obsTakenTime, patient, index } = this.props;
const nextDueTimeForObs = moment(obsTakenTime).add(10, 'minutes');
const nextObservationTime = nextDueTimeForObs.subtract(moment.now()).format('mm');
if (obsTakenTime) {
this.setState({ obsTimeleft: nextObservationTime + ' mins' });
}
if (Number(nextObservationTime) <= 1) {
this.props.disablePatientUpdate();
}
if (Number(nextObservationTime) <= 0) {
this.setState({ obsTimeleft: 'Overdue' });
this.props.enablePatientUpdate();
clearInterval(this.countDownInterval);
() => this.props.resetPatient(patient, index); // here i call the function as call back
}
}
Как установить состояние родительского компонента из дочернего компонента в react.