How to call a method in render instead of componentDidUpdate?

0

I want to avoid componentDidUpdate lifecycle and want to call a method within render. Since I am not loading data in this component I want to get rid of componentDidUpdate.

Below is the code,

export default class child extends React.Component {
    state = {
        query: '',
        data: null,
    };
    empty_id = 0xffffffff;

    componentDidMount() {
        this.set_open_data();
    }

    componentDidUpdate(prevProps) {
       if (prevProps.id !== this.props.id) {
            this.set_data();
       }
    }

    set_data = () => {
        if (!this.props.info) {
            return;
        }
        if (this.props.id === this.empty_id) {
            this.setState({data: null});
            return;
        }

        let data = {
            info: [],
            values: [],
        };
        const info = this.props.info;
        for (let i=0, ii=info.length; i < ii; i++) {
            if (info[i].meshes.includes(this.props.id)) {
                const info = info[i].info;
                const values = info[i].values;
                data = {
                    info: typeof info === 'string' ? info.split('\r\n') : [],
                    values: values ? values : [],
                };
                break;
            }
        }
        this.setState({data: this.filter_data(data, this.state.query)});
      };
      render = () => {
          /* i want to call set_data here but should not sure of when to be 
         called meaning condition like in componentdidupdate method*/
         this.set_data();};}

Could someone help me solve this? Thanks.

reactjs
asked on Stack Overflow Mar 7, 2019 by stackoverflow_user • edited Mar 7, 2019 by Vasilisa

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0