简体   繁体   English

通过 API 返回的数组重复映射子组件内的子组件

[英]Mapping a sub-component within a child-component repeatedly through an array returned by API

I'm having a problem with mapping components.我在映射组件时遇到问题。 Frankly, I'm not experienced enough with React to code like a God.坦率地说,我对 React 的经验不足,无法像上帝一样编码。 I'll walk you through my problem in increments so you guys fully understand what's happening.我将逐步引导您解决我的问题,以便你们完全了解发生了什么。

Annex 1.1 This is my child component, as you can see, I am using React useState and useEffect hooks to update my state when I pull an array from an API.附件 1.1 这是我的子组件,如您所见,当我从 API 中提取数组时,我正在使用 React useState 和 useEffect 钩子来更新我的状态。 No big deal, no problem here, just regular stuff.没什么大不了的,这里没问题,只是普通的东西。

function Financeticker() {

const [crypto, setCrypto] = useState([]);
const classes = useStyles();

useEffect(() => {
    axios
        .get(
            `https://min-api.cryptocompare.com/data/top/totalvolfull?limit=10&tsym=USD&api_key=xxxxxxxxxxxxxxxxxxxx`
        )
        .then((resCrypto) => {
            console.log(resCrypto.data.Data);
            setCrypto(resCrypto.data.Data);
        })
        .catch((err) => {
            console.log(err);
        });
}, []);

Annex 1.2 Here is a function that functions as a subcomponent (fetus component, lol jk) in my child component.附件1.2 这里是一个函数,它在我的子组件中作为子组件(胎儿组件,lol jk)起作用。 If it gets confusing, you'll probably understand at Annex 1.3 but basically the response of the API returns an array of [10] objects.如果它变得混乱,您可能会在附件 1.3 中理解,但基本上 API 的响应返回一个 [10] 对象数组。 Now, I want the subcomponent to render 10 times (using .map) and pull each property from the API (IE {crypto.CoinInfo.FullName} ) The problem is that I get an error stating "Cannot read property "FullName" of undefined.现在,我希望子组件渲染 10 次(使用 .map)并从 API 中提取每个属性(IE {crypto.CoinInfo.FullName} )问题是我收到一个错误,指出“无法读取未定义的属性“FullName” .

const cryptoTicker = crypto.map(() => (
        <Grid item xs={12}>
            <Card className={classes.root}>
                <CardContent>
                    <Typography
                        className={classes.companyName}
                        color='textSecondary'
                        gutterBottom
                    >
                        {crypto.CoinInfo.FullName} <---- CANNOT READ PROPERTY "FULLNAME" OF UNDEFINED
                    </Typography>
                </CardContent>
            </Card>
        </Grid>
    ));

Annex 1.3 Here is the child component's return method that basically calls the {cryptoTicker} function within the Ticker component. Annex 1.3 这里是子组件的返回方法,它基本上调用了Ticker组件中的{cryptoTicker}函数。 In effect, we are doing;实际上,我们正在做; Step 1: Pulling data from an API.第 1 步:从 API 中提取数据。 Step 2: rendering subcomponent X amount of times (in this case, 10 times) within the Ticker child component.步骤 2:在Ticker子组件内渲染子组件 X 次(在本例中为 10 次)。 Step 2.1: pulling that data from the array returned by the API and plonking the data down on the subcomponent.步骤 2.1:从 API 返回的数组中提取该数据并将数据向下放到子组件上。

    return <Ticker>{({ index }) => <>{cryptoTicker}</>}</Ticker>;
}

export default Financeticker;

I hope this question was structured well enough to get an answer.我希望这个问题的结构足够好以获得答案。 Thanks dev community感谢开发社区

Well, you are mapping over an array , but you never specified the item.好吧,您正在映射array ,但您从未指定该项目。 The problem is here in the first line:问题出在第一行:

const cryptoTicker = crypto.map(() => (

Change this to将此更改为

const cryptoTicker = crypto.map((item) => (

Then you should be able to display the name like this:然后你应该能够像这样显示名称:

    {item.CoinInfo.FullName}

Full code:完整代码:

const cryptoTicker = crypto.map((item) => (
        <Grid item xs={12}>
            <Card className={classes.root}>
                <CardContent>
                    <Typography
                        className={classes.companyName}
                        color='textSecondary'
                        gutterBottom
                    >
                        {item.CoinInfo.FullName} <---- CANNOT READ PROPERTY "FULLNAME" OF UNDEFINED
                    </Typography>
                </CardContent>
            </Card>
        </Grid>
    ));

I am making an educated guess, but I think that you had the problem at all, was that you had no loading state and so on the first render, crypto was an empty array , but you tried to access a property of it, as it was an object .我在做一个有根据的猜测,但我认为你有问题,是你没有加载状态等等第一次渲染, crypto是一个空数组,但你试图访问它的一个属性,因为它是一个对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM