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

remove componentWillReceiveProps #394

Conversation

zackseuberling
Copy link
Contributor

closes #355

all tests are still passing, but I'm not entirely sure what is supposed to happen with this life cycle hook, so unsure if this could introduce new bugs

@DelandPo
Copy link

DelandPo commented Feb 1, 2020

We should try to get this in

@MarkEhr
Copy link

MarkEhr commented Feb 4, 2020

Hi @zackseuberling, it looks like there's something that needs to be changed in your code. You can't just change the method name cause getDerivedStateFromProps is a static method. That means you can't use this inside it.
Please take a look a the docs.
The correct solution would be to split the actual method in two. Using getDerivedStateFromProps for calculating the new state and componentDidUpdate to perform side effects like setting up the autoPlay feature.

@zackseuberling
Copy link
Contributor Author

zackseuberling commented Feb 4, 2020

re-reading the original code, it seems like all of componentWillReceiveProps code could be moved to the componentDidUpdate method

React states explicitly in their docs for getDerivedStateFromProps

If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.

@zackseuberling zackseuberling changed the title convert componentWillReceiveProps to getDerivedStateFromProps remove componentWillReceiveProps Feb 9, 2020
@MarkEhr
Copy link

MarkEhr commented Feb 26, 2020

I tried to use your branch but the warning was still there. It looks like you didn't build the project before uploading.

@zackseuberling
Copy link
Contributor Author

zackseuberling commented Feb 26, 2020

I tried to use your branch but the warning was still there. It looks like you didn't build the project before uploading.

I was under the impression that the build would be automated.

@MarkEhr, I ran a build locally, and did get it to work via yarn link in another project.

@akjha96
Copy link

akjha96 commented Mar 3, 2020

Hi, Could you please review this PR... It would be nice of you @leandrowd

@SamShiSS
Copy link

Hi @leandrowd , could you please review and merge this? Thank you!

Comment on lines +120 to +133
if (prevProps.selectedItem !== this.props.selectedItem) {
this.updateSizes();
this.moveTo(this.props.selectedItem);
}

if (prevProps.autoPlay !== this.props.autoPlay) {
if (this.props.autoPlay) {
this.setupAutoPlay();
} else {
this.destroyAutoPlay();
}

this.setState({ autoPlay: this.props.autoPlay });
}
Copy link
Contributor Author

@zackseuberling zackseuberling Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at my code again, I think I may have mixed up some logic about nextProps/prevProps/prevState?

this is emphasizing for me how confusing componentWillReceiveProps is.

this is correct, right:

componentWillReceiveProps:nextProps :: componentDidUpdate:this.props

if (prevState.swiping && !this.state.swiping) {
// We stopped swiping, ensure we are heading to the new/current slide and not stuck
this.resetPosition();
}

if (prevProps.selectedItem !== this.props.selectedItem) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

believe the logic here should actually be (since the component has updated at this point), if this.props.selectedItem does not match this.state.selectedItem, but this is a very confusing thing to write…

this.updateSizes();
this.moveTo(nextProps.selectedItem);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if selectedItem prop is going to change, and it doesn't match this.state.selectedItem… do thing

@leandrowd
Copy link
Owner

Thanks for the contribution :)

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

Successfully merging this pull request may close these issues.

componentWillReceiveProps is marked as deprecated
6 participants