gpt4 book ai didi

javascript - React Redux 状态数组变量作为 prop 传递给子组件,无限循环或空数组

转载 作者:行者123 更新时间:2023-11-30 06:11:03 26 4
gpt4 key购买 nike

我将 Redux 状态变量集合作为 props 传递给 PokeList.js,使用 useEffect Hook 设置状态,但如果我将 props.collection 设置为依赖项数组映射函数将 props.collection 视为空数组,因为我试图在 map 函数中注销 pokeData 并且什么也得不到,如果我删除props.collection 依赖,它创建了一个无限循环的情况,我可以 console.logprops.collection,它首先显示一个空数组,然后是正确的数组,如何正确设置状态?

我试过在 PokeList.js 中调度,结果相同,还尝试直接初始化 cardList = props.collection.map ...但是获取未定义的卡片列表,还尝试使用 React.memo 并将 Prop 设置为依赖项也不行

//PokeList.js
import React, { useState, useEffect } from 'react';
import Card from 'react-bootstrap/Card';
import ListGroup from 'react-bootstrap/ListGroup';

const PokeList = (props) => {
const [cardList, setCardList] = useState();
console.log(props.collection)

useEffect(() => {
var newCardList = props.collection.map(pokeData => {
console.log(pokeData)
return (
<Card key={pokeData.id} style={{ width: '18rem' }}>
<Card.Img variant="top" src={pokeData.sprite} />
<Card.Body>
<Card.Title>{pokeData.Name}</Card.Title>
<ListGroup className="list-group-flush">
<ListGroup.Item>{'Height: ' + pokeData.height}</ListGroup.Item>
<ListGroup.Item>{'Weight: ' + pokeData.weight}</ListGroup.Item>
</ListGroup>
</Card.Body>
</Card>
)})
setCardList(newCardList)
}, [props.collection])

return (
<div>
{cardList}
</div>
)
}

export default PokeList;

//Search.js
import React, { useEffect } from 'react';
import { Container } from 'react-bootstrap';
import { useDispatch, useSelector } from 'react-redux';

import PokeList from './pokedex/PokeList';
import * as pokedexActions from './pokedex/actions/PokedexActions';

const Search = () => {
const dispatch = useDispatch();

useEffect(() => {
dispatch(pokedexActions.getLimitNames(5))
}, [dispatch])

const collection = useSelector(state => state.pokedex.collection);

return (
<div>
<Container>
<h2>Search</h2>
<PokeList collection={collection}/>
</Container>
</div>
);
}

export default Search;

// reducer.js
import { GET_LIMIT_NAMES } from '../actions/PokedexActions';

const initialState = {
collection: []
};

export default (state = initialState, action) => {
switch (action.type) {
case GET_LIMIT_NAMES:
return {
collection: action.data
};
default:
return state;
}
};
// action.js
import Pokemon from '../Pokemon';

export const GET_LIMIT_NAMES = "GET_LIMIT_NAMES";

export const getLimitNames = (limit = 100) => {
// redux-thunk
return async dispatch => {
try {
const allResponse = await fetch(`https://pokeapi.co/api/v2/pokemon/?limit=${limit}`);
const allUrlsData = await allResponse.json();
// console.log(allUrlsData.results);

const collection = [];

Promise.all(allUrlsData.results.map(urlData => {
var pokemon;
fetch(urlData.url).then(resp =>
resp.json()
).then(data => {
// console.log(data);
pokemon = new Pokemon(data);
// pokemon.log();
collection.push(pokemon)
}).catch(err => {
console.log(err);
})
return collection;
}))

// console.log(collection)

dispatch({
type: GET_LIMIT_NAMES,
data: collection
});

} catch (err) {
console.log(err);
}
};
};

如果我尝试直接渲染 map 结果,什么也没有出现, map 功能仍然只得到空数组,

// PokeList.js
import React from 'react';
import Card from 'react-bootstrap/Card';
import ListGroup from 'react-bootstrap/ListGroup';
import './Pokedex.css'

const PokeList = (props) => {
console.log('props.collection', props.collection)

return (
// <div className='poke-list'>
// {cardList}
// </div>
<div className='poke-list'>
{props.collection.map(pokeData => {
return (
<Card key={pokeData.id} style={{ width: "18rem" }}>
<Card.Img variant="top" src={pokeData.sprite} />
<Card.Body>
<Card.Title>{pokeData.Name}</Card.Title>
<ListGroup className="list-group-flush">
<ListGroup.Item>{"Height: " + pokeData.height}</ListGroup.Item>
<ListGroup.Item>{"Weight: " + pokeData.weight}</ListGroup.Item>
</ListGroup>
</Card.Body>
</Card>
);
})}
</div>
)
}

export default PokeList;

enter image description here

如果我删除 [props.collection] 依赖项,它会创建无限循环情况,但组件正在渲染 enter image description here

最佳答案

这里的问题是,每次 Search 呈现时,都会创建 collection 并将其传递给 PokeList。然后 PokeList 正在使用该 collection(记住,每次渲染都是新的)并将其用作 Hook 的依赖项。这意味着每次 Search 呈现时,PokeList 中的 Hook 都会运行。

只需使用 collection 属性来渲染 PokeList 中的组件树:

const PokeList = props => {
console.log(props.collection);

return (
<div>
{props.collection.map(pokeData => {
return (
<Card key={pokeData.id} style={{ width: "18rem" }}>
<Card.Img variant="top" src={pokeData.sprite} />
<Card.Body>
<Card.Title>{pokeData.Name}</Card.Title>
<ListGroup className="list-group-flush">
<ListGroup.Item>{"Height: " + pokeData.height}</ListGroup.Item>
<ListGroup.Item>{"Weight: " + pokeData.weight}</ListGroup.Item>
</ListGroup>
</Card.Body>
</Card>
);
})}
</div>
);
};

关于javascript - React Redux 状态数组变量作为 prop 传递给子组件,无限循环或空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com