简体   繁体   English

不知道如何将 map 阵列与组件反应原生

[英]Not sure how to map array to component in react native

I want to show items of a json array in chart.我想在图表中显示 json 数组的项目。 The array is the following and is in a separate json file-数组如下,位于单独的 json 文件中 -

{
  "milimeters": ["90", "102", "93", "84"],
} 

I want to map this array over a tooltip component I declared in react.我想在我在反应中声明的工具提示组件上 map 这个数组。 I want to show each value of 'millimeters' array upon click -我想在点击时显示“毫米”数组的每个值 - 在此处输入图像描述

Below is code for the component-下面是组件的代码-

import data from '../dummyData.json';
const Tooltips = ({ x, y, data }) => {
                  // console.log(data);
                  return data.map((item, index) => (
                    <Text
                      key={index}
                      x={x(data[index])}
                      y={y(item.milimeters) - 15}
                      fontSize={15}
                      fontWeight="lighter"
                      stroke="#fff"
                      fill="#fff"
                      textAnchor="middle"
                      alignmentBaseline="middle"
                    >
                    {`${item.milimeters}`}
                    </Text>
                  ));
                }; 

This component renders as below- The item.millimeters is showing up as undefined for some reason.该组件呈现如下 - item.mmimeters 由于某种原因显示为未定义。 How can I map item.milimeter value correctly to each bar?如何将 map item.milimeter 值正确应用于每个条? 在此处输入图像描述

You have to map through data.milimeters since it is an array.你必须通过data.milimeters因为它是一个数组。

import data from '../dummyData.json';
const Tooltips = ({ x, y, data }) => {
                  // console.log(data);
                  return data.milimeters.map((item, index) => (
                    <Text
                      key={index}
                      x={x(item.milimeters[index])}
                      y={y(item.milimeters) - 15}
                      fontSize={15}
                      fontWeight="lighter"
                      stroke="#fff"
                      fill="#fff"
                      textAnchor="middle"
                      alignmentBaseline="middle"
                    >
                    {`${item}`}
                    </Text>
                  ));
                }; 

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

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