question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using `Fragment` inside chart

See original GitHub issue

Hi, I’m having an issue with dynamically rendering charts. Basically my data provides on each element information for 2 Line or Bar elements. So I would like to do a data.map and then render 2 Bars or Lines on each iteration. However, React/JSX requires me to wrap these elements in another element so preferably I’d wrap the two elements in a Fragment tag but if I do this nothing shows up in my chart.

My current code looks like this and this works fine:

<LineChart data={data}>
      <XAxis dataKey="timestamp" tickFormatter={tickFormatter} />
      <Tooltip content={renderLineTooltip} />
      {dataKeys.map((dataKey, index) => (
        <Line
          key={`${dataKey}-submitted`}
          strokeWidth={2}
          dataKey={`${dataKey}.submittedCount`}
          stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
          connectNulls
        />
      ))}
      {dataKeys.map((dataKey, index) => (
        <Line
          key={`${dataKey}-delivered`}
          strokeWidth={2}
          dataKey={`${dataKey}.deliveredCount`}
          stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
          connectNulls
        />
      ))}
    </LineChart>

However, preferably I’d like my code to look like this:

<LineChart data={data}>
      <XAxis dataKey="timestamp" tickFormatter={tickFormatter} />
      <Tooltip content={renderLineTooltip} />
      {dataKeys.map((dataKey, index) => (
        <Fragment>
          <Line
            key={`${dataKey}-submitted`}
            strokeWidth={2}
            dataKey={`${dataKey}.submittedCount`}
            stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
            connectNulls
          />
          <Line
            key={`${dataKey}-delivered`}
            strokeWidth={2}
            dataKey={`${dataKey}.deliveredCount`}
            stroke={DEFAULT_CHART_COLORS[index % DEFAULT_CHART_COLORS.length]}
            connectNulls
          />
        </Fragment>
      ))}
    </LineChart>

This would prevent me from having to loop over the same array twice. 😃

Am I doing something wrong or is this kind of behavior not supported for some reason?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
lstuartfrycommented, May 11, 2018

I’ve got this issue as well. It looks like Recharts is using React v16.0. It has not yet upgraded to 16.2 or higher, so it doesn’t support the updated Fragments syntax. It does support the older Fragment syntax:

<AreaChart>
  [
    <Area key={1} />,
    <Area key={2} />,
  ]
</AreaChart>

Just be sure to 1) separate each element with a comma, and 2) give each component a proper ‘key’,

0reactions
xile611commented, Mar 17, 2020

Sorry for not handling your issue in time, please try the latest version. If the problem persists, please open a new issue according to the issue guide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Displaying chart inside Fragment Activity in Android
Fragment activity contains the references of achartengine libraries and return a view.
Read more >
How to use chart inside fragment in Android ... - Stack Overflow
Use the View that you inflate in onCreateView as a starting point to "find" your other views while you're still in onCreateView ,...
Read more >
Creating and Using Fragments | CodePath Android Cliffnotes
Refer to this detailed lifecycle chart to view the lifecycle of a fragment more visually. Looking Up a Fragment Instance. Often we need...
Read more >
Fragments and the Navigation Component - Android Developers
In this codelab, you'll learn about fragments, the fragment lifecycle, ... Graph to navigate between fragments in the same host activity.
Read more >
Add sample how to add map inside another fragment ...
I want to add map inside fragment with custom layout. Currently i found two ways: 1. I can do it using ChildFragmentManager that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found