【发布时间】:2020-07-23 11:04:06
【问题描述】:
我在 React.js 的父组件上加载一个子组件。单击按钮,数据将通过 props 传递给子组件,子组件将映射该数据并呈现在屏幕上。我正在从本地存储中获取数据并将其数据结构处理为子组件。
但是,问题是当我单击按钮并且正在传递和呈现数据时,会显示按钮,并且在呈现子组件后会显示出来。当我单击按钮时,我需要加载微调器,它会消失并显示实际组件。
我在该州尝试过loading: false之类的方法,但无济于事。
感谢您的帮助
import ShowPatientAppointments from './ShowPatientAppointments.component';
class PatientAppointmnent extends Component {
state = {
doctorSlots: null,
timingSlot: null,
bookDay: null,
bookTime: null,
hasTiming: false,
}
getSlots = () => {
let slot = [];
let time = [];
for (let i=0; i< localStorage.length; i++) {
let key = localStorage.key(i);
let value = JSON.parse(localStorage[key]);
slot.push(key);
time.push(value);
this.setState({doctorSlots: slot, timingSlot: time});
}
console.log(this.state.doctorSlots, this.state.timingSlot);
}
render() {
const { doctorSlots, timingSlot, hasTiming } = this.state;
return(
<div>
<button onClick={this.getSlots} className='button'>Show me dates</button>
{doctorSlots === null ? <p></p> : <PatientSelectDay props={doctorSlots} timing={timingSlot} getTimings={this.getTiming} />}
</div>
)
}
}
export default PatientAppointmnent;
class PatientSelectDay extends Component {
state = {
options: [...this.props.props].map(obj => {
return {value: `${obj}`, label: `${obj}`}
}),
timingOptions: [...this.props.timing],
open_id: [],
part_id: '',
doctorDay: 'day',
doctorTiming: 'timing',
}
changeSingleHandler = e => {
this.setState({ part_id: e ? e.value : '' });
};
changeHandler = e => {
let add = this.state.open_id;
add.push(e.map(x => x.value));
this.setState({ open_id: e ? add : [] });
};
saveState = (option) => {
...save selected options
}
render() {
const {options, timingOptions} = this.state;
return (
<div>
<div className='carousel'>
{options.map((option, index) => {
const timing = timingOptions[index].map(obj => {
return {value: `${obj}`, label: `${obj}`}});
return(
<div key={index}>
<Select
name="open_id"
value={option}
onChange={this.changeSingleHandler}
options={option}
className='select'
/>
<Select
name="open_id"
value={this.state.open_id}
onChange={this.changeHandler}
options={timing}
className='select'
/>
<button onClick={() => this.saveState(option)} className='button-left'>Select Days</button>
</div>
)
})}
</div>
</div>
)
}
}
export default PatientSelectDay;
【问题讨论】:
-
将loading设置为false的函数传给孩子,在孩子的
componentDidMount()中调用 -
根据您的逻辑和设计...应该显示加载程序,直到我们没有可用的医生插槽。尽管 hasTiming 状态从未被您使用过,也从未更改为 true ......但是如果我们只按照您的逻辑进行,请在父组件的 render() 中进行一些小的更改,然后添加并查看 LOADING 是否出现在屏幕上。 {医生插槽 === 空?
正在加载 ...
:。你的 Loader Spinner 应该替换 LOADING ...
... 做这个小修改,让我知道 -
我已经做过 {doctorSlots === null ?
正在加载...
。它一直显示加载,直到我按下按钮。但是只有当我按下按钮并显示加载直到加载时我才需要加载。
标签: javascript reactjs react-props react-component react-state