react-vis: Stateless function components cannot be given refs. Attempts to access this ref will fail
Following errors thrown while trying to render a Line Chart from the demo here.
Steps to reproduce
- Create a new react app using CRA
- Add react-vis to dependencies
- Create a Component with following content
import React from 'react';
import {
XYPlot,
XAxis,
YAxis,
HorizontalGridLines,
VerticalGridLines,
LineSeries
} from 'react-vis';
export default class Example extends React.Component {
render() {
return (
<XYPlot
width={300}
height={300}>
<HorizontalGridLines />
<VerticalGridLines />
<XAxis title="X Axis" position="start"/>
<YAxis title="Y Axis"/>
<LineSeries
className="first-series"
data={[
{x: 1, y: 3},
{x: 2, y: 5},
{x: 3, y: 15},
{x: 4, y: 12}
]}/>
<LineSeries
className="second-series"
data={null}/>
<LineSeries
className="third-series"
curve={'curveMonotoneX'}
style={{
strokeDasharray: '2 2'
}}
data={[
{x: 1, y: 10},
{x: 2, y: 4},
{x: 3, y: 2},
{x: 4, y: 15}
]}
strokeDasharray="7, 3"
/>
<LineSeries
className="fourth-series"
data={[
{x: 1, y: 7},
{x: 2, y: 11},
{x: 3, y: 9},
{x: 4, y: 2}
]}/>
</XYPlot>
);
}
}
- Import it to App.js
import React, { Component } from 'react';
import LineChart from './LineChart';
import './App.css';
class App extends Component {
render() {
return (
<LineChart />
);
}
}
export default App;
-
Do a
yarn start
and see the console. -
package.json
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-scripts": "1.1.4",
"react-vis": "^1.10.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
My guess is that the XYPlot
component in function _getClonedChildComponents
is adding ref
to the props of the child components, while the children can easily be SFCs (In the example above the components HorizontalGridLines
,VerticalGridLines
,XAxis
,YAxis
are all SFC inside XYPlot
component, hence the 4 exceptions) and in that case the exception is thrown.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 17 (8 by maintainers)
@mcnuttandrew on it π π¨
Should π be fixed in 1.10.3
I updated to 1.10.4 and itβs all OK here π
Thank you @benshope and thank you @markov00 π
Looks like that fixed it. Thanks @benshope and @mcnuttandrew for the work on this.
Interesting, okay, re-opening