简体   繁体   English

组件不会在地图功能上渲染

[英]Component Won't Render on Map Function

I'm using this component progress to draw a progress line. 我正在使用此组件进度来绘制进度线。 It works when I hardcode the values, but when I use it inside a map function it does not pick up the values. 当我对值进行硬编码时,它可以工作,但是当我在map函数中使用它时,它不会拾取值。

Can someone explain why this doesn't work?. 有人可以解释为什么这行不通吗?

import { Line, Circle } from 'rc-progress';

render(){ 

  const elements = [{'percent':10,'color':'#3FC7FA'}, {'percent':30,'color':'#85D262'},{'percent':80,'color': '#FE8C6A'}]
return (
  {elements.map((obj) => 
      <Line percent="{obj.percent}" strokeWidth="6" strokeColor="{obj.color}" trailWidth="6" /> 
   )}
)
}

You missed the return statement: 您错过了return语句:

render(){ 

  const elements = [{'percent':10,'color':'#3FC7FA'}, {'percent':30,'color':'#85D262'},{'percent':80,'color': '#FE8C6A'}]
  return elements.map((obj) => 
      <Line percent="{obj.percent}" strokeWidth="6" strokeColor="{obj.color}" trailWidth="6" /> 
   )
}

Your render needs a return 您的渲染需要退货

import { Line, Circle } from 'rc-progress';

render(){ 

    const elements = [{'percent':10,'color':'#3FC7FA'},{'percent':30,'color':'#85D262'},{'percent':80,'color': '#FE8C6A'}]
    return elements.map((obj) => 
        <Line percent={obj.percent} strokeWidth="6" strokeColor={obj.color} trailWidth="6" /> 
}

The elements.map() isn't a single value, so, you should not use it inside {} . elements.map()不是单个值,因此,不应在{}使用它。 Just return the map on your render() method 只需在您的render()方法上返回地图

Pay atention: if you want to pass variables to your components props, don't use quotes. 注意:如果要将变量传递给组件prop,请不要使用引号。 Do this: <Line percent={obj.percent}/> instead of <Line percent="{obj.percent}"/> 这样做: <Line percent={obj.percent}/>而不是<Line percent="{obj.percent}"/>

Take a look at https://reactjs.org/docs/components-and-props.html#rendering-a-component 看看https://reactjs.org/docs/components-and-props.html#rendering-a-component

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

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