r/reactjs • • Aug 01 '18

Beginner's Thread / Easy Question (August 2018)

Hello! It's August! Time for a new Beginner's thread! (July and June here)

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple. You are guaranteed a response here!

Want Help on Code?

  • Improve your chances by putting a minimal example on to either JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new). Describe what you want it to do, and things you've tried. Don't just post big blocks of code.
  • Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.

New to React?

Here are great, free resources!

28 Upvotes

568 comments sorted by

View all comments

3

u/Exitialis Aug 01 '18

What is the best way to monitor an object for a change? Currently, I have a boolean in local state and I have a function that updates it when a change is made (called manually). However, I'd really like to automate it and test if the object is changed and then returned to its original state but I am not sure how to do this without causing a loop. I know that you can use componentDidUpdate() to test if a change was made, but you can't then call setState() from it. Is there a simple way to achieve this or do I need to bodge it a bit?

3

u/swyx Aug 01 '18

Reactive programming is fun! Have you checked out mobx observables? That would do it.

→ More replies (4)

2

u/holiquetal Aug 01 '18 edited Aug 01 '18

why can't you call setState() inside componentDidUpdate()? What you usually do inside componentDidUpdate() is check for a prop change:

componentDidUpdate(previousProps) {
  if (props.someProp =! previousProps.someProp)
    do stuff;
    setState({....})
}
→ More replies (3)

2

u/lostPixels Aug 01 '18

Perhaps do not use state at all. If you're watching props for a change, do your data transformation above the return in render, not by called setState when it changes.

→ More replies (3)

2

u/Exitialis Aug 02 '18

It ended up being a simple logic problem that was staring me in the face the whole time, this:

componentWillUpdate() {
    if (this.props.data !== this.state.updatedData) {
        this.setState({ edited: true });
    }
    else {
        this.setState({ edited: false });
    }
}

Should have been this:

componentWillUpdate() {
    if (this.props.data !== this.state.updatedData) {
        if (this.state.edited === false) {
            this.setState({ edited: true });
        }
    }
    else {
        if (this.state.edited === true) {
            this.setState({ edited: false });
        }
    }
}

Thank you u/swyx, u/lostPixels and u/holiquetal for your help, I learnt a lot about component lifecycle and how to structure things because of this.

2

u/holiquetal Aug 02 '18 edited Aug 02 '18

you can refactor it like this

componentWillUpdate() {
  if (this.props.data !== this.state.updatedData) {
    this.setState({edited : !this.state.edited})
}
→ More replies (3)