Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么React不呈现我的组件数组?_Reactjs - Fatal编程技术网

Reactjs 为什么React不呈现我的组件数组?

Reactjs 为什么React不呈现我的组件数组?,reactjs,Reactjs,我有一个JSON API响应,我想将其映射到一个名为的子组件列表中,如下所示: render() { // ... other code ... let res_list = []; fetch(request) .then(response => response.json()) .then(reservations => { console.log(reserva

我有一个JSON API响应,我想将其映射到一个名为
的子组件列表中,如下所示:


    render() {
        // ... other code ...

        let res_list = [];

        fetch(request)
        .then(response => response.json())
        .then(reservations => {
            console.log(reservations)
            res_list = reservations.map((res) => <ReservationBox key={res.id} court={res.court} date={res.day} start_time={res.start} end_time={res.end} />);
            console.log(this.res_list);
        });

        return (
            <div>
                {heading}
                {res_list}
            </div>
        );
    }

render(){
//…其他代码。。。
让res_list=[];
提取(请求)
.then(response=>response.json())
。然后(预订=>{
console.log(预订)
res_list=reservations.map((res)=>);
console.log(这个.res_列表);
});
返回(
{标题}
{resu list}
);
}
现在,我的
reservations
记录正确,并且
res\u列表作为React组件数组正确显示在控制台中,但它不会在父组件中呈现


任何想法或帮助都将不胜感激,因为我在这方面找不到任何东西。提前谢谢

res\u list
不可用于您要返回它的位置。在
then()
块中,将返回的数组置于状态,然后在返回块中使用此状态。

fetch
是异步的。在执行
return…
并评估
res\u list
的那一刻,对
res\u list
的赋值尚未发生。看见仔细想想,每次组件呈现时发出网络请求在概念上是错误的。您应该发出网络请求以响应某些事件,更新组件的状态,这将导致组件重新启动。我不使用react,但您不需要使用类似于
let[res\u list,setList]=useState([])
的内容,然后将其设置为<代码>设置列表(res_list)
中的
。然后()
块?非常感谢@FelixKling!我还注意到,在
render
方法中使用
fetch
的网络请求大约有数百万个-将其放入
componentDidMount()