简体   繁体   English

将道具传递给嵌套对象-React.js

[英]Pass props to nested object - Reactjs

I am trying to pass props to the different fields below to populate a chart. 我正在尝试将道具传递到下面的不同字段以填充图表。 However, I get this error from React when it attempts to render: 但是,当我尝试渲染时,我从React得到这个错误:

Parsing error: Unexpected token, expected ","

Here is my code: 这是我的代码:

function BarChart(props) {
        return (
            <Bar
                data={{
                    labels: [{props.chartTitle}],
                    datasets: [
                        {
                            label: {props.timeFrames},
                            backgroundColor: {props.chartBGColor},
                            data: {props.data}
                        }
                    ]
                }}
            />
        )
}
export default BarChart;

When I pass props like this, I don't get the error. 当我通过这样的道具时,我不会得到错误。 I just don't get the chart rendered correctly. 我只是无法正确显示图表。

function BarChart(props) {
            return (
                <Bar
                    data={props.data}
                />
            )
    }
    export default BarChart;

The wrapping curly braces around the props values are invalid. props值周围的花括号大括号无效。 You need to remove them, or add keys if they are intended to be objects: 您需要删除它们,或者如果打算将它们用作对象,则添加键:

function BarChart(props) {
  return (
    <Bar
      data={{
        labels: [props.chartTitle],
        datasets: [
          {
            label: props.timeFrames,
            backgroundColor: props.chartBGColor,
            data: props.data
          }
        ]
      }}
    />
  );
}

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

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