Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XYChart] SSR does not work on XYChart #1478

Open
nagiek opened this issue Apr 25, 2022 · 2 comments · May be fixed by #1480
Open

[XYChart] SSR does not work on XYChart #1478

nagiek opened this issue Apr 25, 2022 · 2 comments · May be fixed by #1480

Comments

@nagiek
Copy link

nagiek commented Apr 25, 2022

Hello,

I wanted to report that a small logic error stops SSR from working for XYchart.

In useScales line 43, x/yScale will be returned as undefined if there are no values in the dataRegistry. This stops render of all children of XYChart (Axis, Grid, etc.).

In withRegisteredData line 56, the Series component will not be rendered if there is no x/yScale. This is done on line 48 in a useEffect block. I would propose moving the initial registerData call out of useEffect.

@williaster
Copy link
Collaborator

Hi @nagiek thanks for checking out visx.

Yes, the implementation of the data registry (whereby child components register their data in the parent chart context) with useEffect does prevent XYChart from being server-side rendered.

would propose moving the initial registerData call out of useEffect

There is no hook that is analogous to the legacy class component method componentWillMount. we could consider adding something a custom hook like the following as a workaround:

const useComponentWillMount = (cb) => {
    const willMount = useRef(true)
    if (willMount.current) cb()
    willMount.current = false
}

@nagiek
Copy link
Author

nagiek commented Apr 25, 2022

Thanks for quick response! Loving visx so far, especially XYChart. The reason I wanted it was because I wanted SVG for SSR charts, so I moved away from . My non-XYcharts work very well.

Hmm maybe that idea could work.. The DataProvider scales are only known after the render… would that imply using useComponentWillMount everywhere?

The other idea (might be a breaking change), would be to define all data upfront in the DataProvider, and have each Series component link into the dataRegistry via DataKey. This could be opt-in behaviour?

@nagiek nagiek linked a pull request Apr 26, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants