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.

ReactJS - setState causes viewer to re-render every time

See original GitHub issue

When using Resium with ReactJS and adding an ImageLayer, it is causing the viewer to refresh every time any setState is called. How can I use setState to update certain parts of the page without re-rendering the map?

Example below and screenshot of the issue

` class LiveMap extends React.PureComponent { viewerRef = React.createRef(); state = { name: “test” };

handleSelectedEntityChanged = (event) => {
    console.log("selected: " + event);
    this.setState({
        ...this.state,
        name: this.state.name === "test" ? "testt" : "test"
    });
};

render() {
    const defaultImageryProvider = new UrlTemplateImageryProvider({
        url : "https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png"
    });
  return (
    <Viewer
      full
      ref={this.viewerRef}
      onSelectedEntityChange={this.handleSelectedEntityChanged}>

    <ImageryLayer imageryProvider={defaultImageryProvider} />

    <Entity
        name={this.state.name}
        position={Cartesian3.fromDegrees(61, 130, 100)}
        point={{ pixelSize: 10 }} />
    </Viewer>
  );
}

}; `

b873b053-5879-44ef-bee9-789d10415e80

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rot1024commented, Jun 9, 2021

imageProvider prop of ImageryLayer component is read-only and is not changeable because ImageProvider of ImageryLayer of Cesium API is also read-only.

If the imageProvider is static, you can use outside const variable or useMemo hook as described in this page.

If you want to change the imageProvider dynamically, you cannot avoid regenerating the ImageryLayer. If you want to completely suppress warnings, key props can be used:

const imageProvider = useMemo(() => new UrlTemplateImageryProvider({ url }), [url]);

return <ImageryLayer key={url} imageryProvider={imageProvider} />;

We are also considering suppressing the warnings. Either way, please understand that the imageProvider is read-only.

1reaction
chingiz19commented, Aug 17, 2020

I think I have the same issue. When I update variable in parent that holds Entities as children, they all get redrawn

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS - Does render get called any time "setState" is called?
React doesn't compare state data. When setState is called, it marks the component as dirty (which means it needs to be re-rendered). The ......
Read more >
setState causes viewer to re-render every time · Issue #372 ...
When using Resium with ReactJS and adding an ImageLayer, it is causing the viewer to refresh every time any setState is called. How...
Read more >
React doesn't always trigger a re-render on setState
For new React developers, it is common to think that setState will trigger a re-render. But this is not always the case. If...
Read more >
When does React re-render components? - Felix Gerschau
The reasoning behind this is that React re-renders a component if its state changes, but it doesn't re-render the props if they don't...
Read more >
Just Say No to Excessive Re-Rendering in React - GrapeCity
Re-renders occur when a component's state or prop changes. When neither changes, no re-render occurs. Just like the initial render, a re-render ......
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