2

I am getting this error I guess due to bursting up the react's state but I don't know what is wrong going on here.

So I have parent-child relationship components as you can see below:

PARENT Component

class App extends Component {
    constructor(props){
        super(props);
        this.state={
            parentTime: 0,
        }
    };
    
    
    changeTimeState(childTime){
        //this.setState({parentTime: childTime})
        console.log("Parent state time: ",childTime)
    }
    render() {
        return (
            <div className="box">
                <Child time={this.state.parentTime} changeTime={this.changeTimeState.bind(this)}/>
            </div>
        );
    }
}

CHILD Component

class Child extends Component {
    constructor(props){
        super(props)
        this.state = {
            childTime: props.time
        }
    }

    componentDidMount(){
            if (this.state){
                setInterval(()=>{
                    this.setState({childTime: this.state.childTime+1})
                },1000)
            }
        }
    
    render() {
        return (
            <div newTime={this.props.changeTime(this.state.childTime)}/>
        );
    }
}

I am passing data in between them and uncommenting this.setState({parentTime: childTime}) in parent component causes that error. This is where I need help to understand and fix it.

6
  • 2
    This is because you call a function that changes state directly when passing it as some kind of prop to the div: <div newTime={this.props.changeTime(this.state.childTime)}/> ... a function that changes state should nnot be called like that. It should only be called on user interaction or at certain intervals. What are you trying to do with this? Commented Mar 30, 2021 at 22:09
  • 2
    To better explain what is happening...when you call the function like that the state of the parent component will change so it will re-render...with it also re-rendering the child component, which will call the function again changing the state of the parent component...which will re-render---and so we have an infinite loop Commented Mar 30, 2021 at 22:12
  • What are you trying to have your components do here? Commented Mar 31, 2021 at 7:33
  • Hello @OliverRadini I want my timer to run in background and I am using child component for this because if I set componentDidMount() in parent component then it rerenders all the other components too which is why I tried to make seperate component to overcome that. (I dont know if there is any other way to do it) Commented Mar 31, 2021 at 9:00
  • Hi @TalmacelMarianSilviu thanks for taking your time. My timer is running in background so there can be no user interaction. How can I do it at certain intervals? Commented Mar 31, 2021 at 9:01

1 Answer 1

1
i don't know is it what you wanted 

class Child extends Component {
  constructor(props) {
    super(props);
    this.state = {
      childTime: props.time,
    };
  }

  componentDidMount() {
    if (this.state) {
      setInterval(() => {
        this.setState({ childTime: this.state.childTime + 1 });
        this.props.changeTime(this.state.childTime);
      }, 1000);
    }
  }

  render() {
    return <div />;
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hello @BogdanPryvalov, yes this is exactly what I wanted. But it is giving me undefined when I set it to parent state with the the function changeTime().
And is there any way by which I do not have to re-render all other components when state is updated but my timer should always be running in the background?
i just don't understand why do you need state in child comp what it should count and what the difference between childTime and parentTime in the end?
yes you are right. I actually forgot to change that while taking out the specific piece of code from my complete long code

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.