Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metal Lifecycle Method equivalent of componentWillReceiveProps() #106

Closed
AngeloYoun opened this issue May 3, 2016 · 9 comments
Closed

Comments

@AngeloYoun
Copy link

We have a case where we want to be listening for a change when a config has been updated to a new value.

In React, the way this was done was through listening for any changes to props and then identifying if the prop being changed is different to the old.

componentWillReceiveProps(nextProps) {
    if (JSON.stringify(this.props.foo) !== JSON.stringify(nextProps.foo)) {
        this.changeAction();
    }
}

I am not convinced that this is the best way of doing this so any alternatives to including a lifecycle method would also be fine.

Thank you!
Angelo Youn

@cirocosta
Copy link

I'm not the best to answer this but as Component extends State and from State's test you can check that you can listen to a change, that's probably the equivalent of what you want .

that's my 2 cents, @mairatma can certainly provide you a better answer 🙈

@yuchi
Copy link
Contributor

yuchi commented May 4, 2016

The main difference is between listening to the change of a key to a lifecycle phase between injection of new values and application of those.

@yuchi
Copy link
Contributor

yuchi commented May 4, 2016

The nearer you can get is by using configChanged

@mairatma
Copy link
Contributor

mairatma commented May 4, 2016

Oh yeah, I had forgotten about this event, thanks for mentioning it @yuchhi. Maybe the name is not very intuitive, but it will also be called on the first time that config is set. Do you guys think it's worth having the lifecycle version as well, or is this event good enough?

@AngeloYoun
Copy link
Author

Oh thanks for that, I think that will suite my purpose fine. Thank you!

@yuchi
Copy link
Contributor

yuchi commented May 7, 2016

@AngeloYoun since AFAIK Metal doesn’t differentiate between state (internal attributes) and props (public attributes) you can potentially create an infinite loop by updating the configuration during a configChanged event.

@mairatma
Copy link
Contributor

mairatma commented May 9, 2016

I'll close this since the event is covering the desired use case :)

@mairatma mairatma closed this as completed May 9, 2016
@natecavanaugh
Copy link

@yuchi I just wanted to comment that the way you described the state/props in React is probably the most coherent and concise way of describing them that I've read online :)

@yuchi
Copy link
Contributor

yuchi commented May 12, 2016

Ahahah thank you @natecavanaugh!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants