React component not re-rendering when state changes

Adrian Pascu

I have a react component that based on the url should import some data and then display it in another child component. When the page first loads it loads the initial data as the component state in componentDidMount(). As for further url changes, they are handled in componentDidUpdate() export default class Info extends React.Component{

constructor(props){
    super(props);
    this.state={
        element:null
    }
}

componentDidMount(){
    switch(this.props.match.params.id){
        case 'legende': import('../data/legends.json').then((data)=>{
            this.setState({
                element:(<InfoDisplay data={data.content} />)
            })
        });break;
        case 'istorie': import('../data/history.json').then((data) => {
            this.setState({
                element: (<InfoDisplay data={data.content} />)
            })
        }); break;
    }
}

componentDidUpdate(prevProps){
    if (this.props.match.params.id !== prevProps.match.params.id)
        switch (this.props.match.params.id) {
            case 'legende': import('../data/legends.json').then((data) => {
                this.setState({
                    element: (<InfoDisplay data={data.content} />)
                })
            });
            break;
            case 'istorie': import('../data/history.json').then((data) => {
                this.setState({
                    element: (<InfoDisplay data={data.content} />)
                })
            }); break;
        default: break;
        }
}

render(){
    return (
        <div style={{backgroundImage:`url(${background})`,height:'100vh',overflowX:'hidden',backgroundSize:'cover'}}>
            <Navbar/>
            {this.state.element}
        </div>
        )
    }
}

My problem is that in spite of the state updating in the switch statement, the component won't re-render. I have no idea what's the problem with my approach. Can anyone help me out, please? Thanks!

Edit: Here is the code with shouldComponentUpdate() instead of componentDidUpdate :

import React from 'react'
import * as background from '../assets/img/background_main.png';
import Navbar from './Navbar'
import InfoDisplay from './InfoDisplay';

export default class Info extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            element: null
        }
    }

    componentDidMount() {
        switch (this.props.match.params.id) {
            case 'legende': import('../data/legends.json').then((data) => {
                this.setState({
                    element: (<InfoDisplay data={data.content} />)
                })
            }); break;
            case 'istorie': import('../data/history.json').then((data) => {
                this.setState({
                    element: (<InfoDisplay data={data.content} />)
                })
            }); break;
        }
    }

    shouldComponentUpdate(nextProps,nextState) {
        if (this.props.match.params.id !== nextProps.match.params.id && nextProps) {
            switch (nextProps.match.params.id) {
                case 'legende': import('../data/legends.json').then((data) => {
                    this.setState({
                        element: (<InfoDisplay data={data.content} />)
                    })
                }); return true;
                case 'istorie': import('../data/history.json').then((data) => {
                    this.setState({
                        element: (<InfoDisplay data={data.content} />)
                    })
                });return true;
                default: return false;
            }
        }

        return true;
    }

    render() {
        return (
            <div style={{ backgroundImage: `url(${background})`, height: '100vh', overflowX: 'hidden', backgroundSize: 'cover' }}>
                <Navbar />
                {this.state.element}
            </div>
        )
    }
}
Yoshimitsu

You probably want to use shouldComponentUpdateinstead, 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/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React.js component not re-rendering children when the useState hook changes state

react is not re-rendering component inside array.map, when state of array changes

How to stop react re-rendering component, if part of the state changes?

Redux react not re-rendering component after state changes

Which components are re-rendering when a state on React Redux changes

React component not re-rendering when array prop changes

React Component does not re rendering when updating an object in state hooks

React component doesn't re-render when state changes

React Class component inside Function component is not re-rendering after state changes

How to prevent re-rendering the component which is inside router, when the state of super component changes?

React component not re-rendering on state change

React function component state not re-rendering?

React not re-rendering component after page refresh, even if the state changes

React: how to avoid re-rendering a component with a hook that returns always the same value but changes its inner state

Component not re-rendering when state is changed

How do I prevent an entire list from re-rendering when state of one component changes?

React component create a new instance of the state when re-rendering even the state is not changed

Child component not re rendering after Parent state changes

React route component re-renders when the parent component state changes

React-alert library does not seem to be re-rendering when state changes

Preventing react-redux from re-rendering whole page when state changes

React component is rendering but not updating when state is updating

React component not re-rendering on component state change

React-Redux : Child component not re-rendering on parent state change when using connect() in child

React Component not updated when state changes

Testing react component when state changes

Vue.js not re-rendering when file state changes

How to not re-render a child component when a parent's state changes in React?

React - How do i force child components to re render when parent state component changes?