score:1
You probably want to use shouldComponentUpdate
instead, as what you want is to do stuff before the component is re-rendered.
Check this out: https://code.likeagirl.io/understanding-react-component-life-cycle-49bf4b8674de
and this: http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
score:-1
if you want to re-render on any update then you should use
componentWillReceiveProps(nextProps){
this.setState({element:nextProps.data})
}
score:0
componentDidUpdate() will be invoked if a re-rendering is complete => In fact like you said there is no re-rendering because you never invoke any setState Method.
I'm not very sure if a state can hold a JSX content.
Maybe try it like this:
this.setState({
element: data.content
})
==
<div style={{backgroundImage:`url(${background})`,height:'100vh',overflowX:'hidden',backgroundSize:'cover'}}>
<Navbar/>
<InfoDisplay data={this.state.element} />
</div>
hope you find your bug with this information
score:1
You can really simplify this and make it cleaner, and this should also fix your problem:
In your render do:
return (
<div style={{backgroundImage:`url(${background})`,height:'100vh',overflowX:'hidden',backgroundSize:'cover'}}>
<Navbar/>
<InfoDisplay data={this.state.data} />
</div>
)
Then in your componentDidMount and componentDidUpdate all you have to do is set the state data to data.content from your request:
case 'legende': import('../data/legends.json').then((data) => {
this.setState({
data: data.content,
});
});
obviously you will need to change your constructor as well to be have data:
this.state = {data: null};
Hope this helps.
Source: stackoverflow.com
Related Query
- React Component not updated when state changes
- Component is not re rendering when state is changed in react
- React jsx array component does not re-render when state changes
- Input element's value in react component is not getting re-rendered when the state changes
- react is not re-rendering component inside array.map, when state of array changes
- React component not re-rendering when state changes
- React component is not getting updated when state changes into the redux
- React Video Component not updating when state changes
- React Component does not re rendering when updating an object in state hooks
- React does not re-render component when the state changes
- changes not showing up in state when using TinyMCE in my React Component
- React Component not re rendering when the state got updated
- When state changes the right component is not getting displayed in React
- Page component is not rendering again even when the app state changes in the store
- React component not updating when store state has changed
- React shouldComponentUpdate() is called even when props or state for that component did not change
- React leaflet center attribute does not change when the center state changes
- Updating the react component state when a global variable changes
- React component not rerendering when props changes
- React component not rendering when using react-rails gem
- React component not rendering on state update
- React component rendering even when there is no change in the state value
- Why React component get re-rendered only once when state does not change?
- How to re-render only one component on hover when state changes in React
- React.js component not re-rendering children when the useState hook changes state
- react router not rendering the component when using redux
- Functional Component does not rerender after state changes [ React ]
- React component not re-rendering when "Date" in state gets updated
- HighCharts not updating when the state changes in React
- React class does not update when state changes
More Query from same tag
- parsing json response in react native
- Updating Child Component Via Change of Props from Parent in React
- In react, how do I close a popup confirmation?
- React JS: Expected a string but got: object
- Page not changing onClick using useNavigate in React?
- How to find a child component from the enzyme mounted, react-redux stateToProps connected parent?
- React-chosen raising error
- React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components)
- How to send back a parameter to a function pass as a param in react
- How to pass parameters into a function in Context Reactjs
- How to call getServerSideprops with useEffect in Next.js
- react/javascript - TypeError: Cannot read property 'classList' of null
- I'm fetching some data from an api and when I'm logging the fetched data on the console it get's logged again and again
- Spring Boot with ReactJS
- How to clear entire react material ui table conent
- firebase export issue in react
- Show character limit under input in React Js
- CodeSandbox.io <img> tag not loading image
- Nextjs Warning: Expected server HTML to contain a matching <div> in <div> when using conditional rendering
- For some reason after mapping onClick event on <li> won't work
- Unable to refresh svg chart on Data Change
- Child to parent communication in React (JSX) without flux
- I want to update the state object in immutable manner so that react and redux will work properly?
- Handling unauthorized request in react-query
- how can I scroll component when we are in mobile response
- In create-react-app, adding Bootstrap 4?
- Image slider app in Reactjs using two child components
- Expansion Panel with Text Field input change the panel's back color on input focus
- Material-UI LinearProgress bar not working
- React state changes but background does not change