简体   繁体   English

在反应中渲染地图的大小

[英]render a map's size in react

I have two maps that I want to use their length as an integer for a component in react.我有两个地图,我想将它们的长度用作反应组件的整数。 the code looks something like this:代码如下所示:

function Test() {
  const classes = useStyles();
  const [spacing, setSpacing] = React.useState(2);
  // FETCH REQUEST DATABASE ITEMS
  const [items, setItems] = useState([]);
  useEffect(() => {
    fetch("/test")
      .then(res => res.json())
      .then(items => setItems(items));
  }, []);
  // ASSIGN ITEMS TO MAPPED GRID
  return (
    <Grid
      container
      direction="row"
      justify="space-around"
      alignItems="flex-start"
      spacing={2}
    >
      {items.map(x => {
        return (
          <Grid item sm key={`key${x._id}`}>
            <SmallCard
              key={x._id}

              // MAP I WANT TO TURN INTO INTEGER BELLOW

              mappedItemCount = {x.map.size}

              // ^ ^ ^ ^ ^ ^ THIS PART HERE IN PARTICULAR


            />
          </Grid>
        );
      })}
    </Grid>
  );
}

For some reason this does not seem to turn it into an integer and still lists as NAN.出于某种原因,这似乎并没有将它变成一个整数,仍然列为 NAN。 What am I doing wrong?我究竟做错了什么?

my item was an object not a map due to the way its being rendered.由于呈现方式,我的项目是一个对象而不是地图。 so the correct syntax should really be the following:所以正确的语法应该如下:

mappedItemCount = {Object.keys(x.map.size).length}

Sacred texts can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys神圣文本可以在这里找到: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

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

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