1

Here I'm trying to get value from DefaultOpts.jsx and update the values to setState in Filters.jsx. But I'm getting error as below :

setState(...): Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.

Filters.jsx

import React from 'react';
import DefaultOpts from 'DefaultOpts.jsx';

export default class Filters extends React.Component {

    constructor(props) {
        super(props);        
        this.state = {
            vOptions : []
        }
        this.handleOptions = this.handleOptions.bind(this)
    }

    handleOptions(params) {
        console.log(params)
        this.setState({
            vOptions : params
        });
    }

    componentDidMount() {
    }

    componentDidUpdate() {
    }

    render() {

        return (

            <div> 

                <DefaultOpts handleOptions={this.handleOptions.bind(this)} />

            </div>
        )
    }
}  

DefaultOpts.jsx

import React from 'react';

class DefaultOpts extends React.Component{

    constructor(props) {
        super(props);
    }

    componentDidMount() {        
    }

    componentDidUpdate() {

    }

    render() {

        var optArray = "";

        $.ajax({
            type: "get",
            url: "url-path",
            success: function(data) {
                optArray = data;
            }
        });

        return (
            <div>
                {this.props.handleOptions(optArray)}
            </div>
        )
    }
}

export default DefaultOpts;

I got some answers in stackoverflow but I'm not able to get what's issue in my code. Please suggest me here what's wrong in my code..

1
  • 1
    The problem is that you are invoking handleOptions in the render method of DefaultOpts. This means that that function will be called during render. While you definitely can call a function inside the render, what you cannot do is setState during render, which is what handleOptions does in the parent. Commented Jun 1, 2017 at 11:23

3 Answers 3

1

You can't call this.props.handleOptions inside the render because it will trigger setState of the parent component - and you are still inside the rendering process. That's why it complains.

Try to execute this function inside the componentDidMount (together with your ajax call)

Sign up to request clarification or add additional context in comments.

Comments

1

There are several problems with your code:

1) First and main one that results in the mentioned error is the fact that by calling handleOptions in render you are calling setState that in turn starts react life cycle. This is a really bad practice and always should/can be avoided.

2) You have one more async call to $.ajax in render that does not directly result in updating state but still considered a bad practice.

To conclude - your render function must not result in any app logic being performed, its task is to render results that have already been prepared. Do all heavy/async work in componentDidMount/componentDidUpdate and you will be fine.

1 Comment

nice, but (1) is a big no-no - not just a "bad practice".
-1

render will execute before didMount... so you are setting the state before it is mounted

anyway move the $.ajax call to didMount, you shouldn't be doing logic things in render()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.