The team is now working on the WordPress Interactivity API. This unblocks the same UX Frontity framework enabled but directly in WordPress Core, fully compatible with the new Site Editor.
I keep working on my personal project to learn as much as I can Frontity to be possible to use it in my current job, and I’m facing a new issue that I can’t find a way to fix. I hope you have the experience and knowledge to give me some help.
Description of your issue:
After a component is updated, on Safari and Firefox (Mac) the page changes its scroll position. This component is a D3 graphic.
To reproduce the error:
Open the app on Safari or Firefox (Mac).
Scroll to the bottom of the page.
Filter the graphics by mode or season (there is a sticky bar on top).
You will see the page scrolls a little bit to the top. And always stays there for the next filter changes. Once you scroll down again and try to filter, the page is going scroll too again.
Sorry, everyone, I have found the issue. It’s my limitation with React (still learning).
The lines highlighted below fixed the problem.
Thank you, Fernando
mmczaplinski7
Hi @nandotess85
I’ve cloned your repo and looked at your RadarChartD3 component, but I’m afraid that the issue that you’ve facing has nothing to do with Frontity, but rather with the fact that you’re re-creating the innerHTML every time you call createChart()! The browser will then scroll up in order to accomodate that space that has been just destroyed.
You can just add a fixed height of 500px to your wrapper component and it solves the issue:
Also, if I may, I really really recommend that you don’t assign to this.state inside of your render functions! Calling setState re-renders your component without the need for calling forceUpdate().
To do this you can move your onMouseOver and onMouseOut functions into the component scope and call the setState inside of them like:
class RadarChartD3 extends Component {
...
// On legend item mouse over
onMouseOver = (event) => {
event.preventDefault();
// Label hovered
const index = Array.from(event.target.parentNode.children).indexOf(event.target);
this.setState(state => {
return { highlight: [
...state.highlight.slice(0, index),
true,
...state.highlight.slice(index+1)
]}
});
};
render() {
// .. do the render
}
I have made the changes you recommended on the state part, and it’s working as before, but now using properly React state. Thanks once again.
But for the scroll part (my original issue), I have implemented the solution I have posted before, where I’m using getSnapshotBeforeUpdate. I prefer this one since I want to avoid have the size of the graphic hardcoded (OR duplicated).