-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Value of opacity in TouchableOpacity properly react to state change #32477
Conversation
Hi @hetanthakkar1! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
PR build artifact for 0ce0496 is ready. |
Base commit: 48f6967 |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
PR build artifact for 9f323c4 is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code analysis results:
eslint
found some issues. Runyarn lint --fix
to automatically fix problems.
PR build artifact for bb78149 is ready. |
I could have just compared the opacity in componentDidUpdate instead of the whole style object but it results in failure of test cases due to flow error. This error is an issue in the flow library and has been reported a few years back |
I don't know why the test failed can someone please help me! |
@sota000 Can you please review!? |
@@ -267,6 +267,11 @@ class TouchableOpacity extends React.Component<Props, State> { | |||
if (this.props.disabled !== prevProps.disabled) { | |||
this._opacityInactive(250); | |||
} | |||
if (this.props.style !== prevProps.style) { | |||
this.setState({ | |||
anim: new Animated.Value(this._getChildStyleOpacityWithDefault()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line 176 we already tried to set the opacity with animation on press out. I am wondering why that is not working on the first button press. Have you found out why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested locally and understood the timing issue here. I've tried using setTimeout to wrap the above linked line as alternative (which works), but that is less ideal.
Your approach here makes sense. However, a better way to fix this is to follow https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html and refactor this class to a functional class. It's possible to use a local variable to host opacity and pass that down.
I don't think the failures are related to your changes. Could you rebase and try again? |
@ryancat Sure! Thanks |
@ryancat I'm trying to figure out why opacity didn't set on line 176 |
bb78149
to
b451b74
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code analysis results:
eslint
found some issues. Runyarn lint --fix
to automatically fix problems.
b451b74
to
ee17d78
Compare
@ryancat Actually on line 176 we tried to set the opacity in I am a bit skeptical about my approach but this solution does seem to work so I am waiting for your take on this approach and once you feel that this is correct I can convert this to a functional component. |
PR build artifact for ee17d78 is ready. |
Can someone please review my pull request |
Thanks for the investigation! I am a bit confused though -- if we documented that The reason why that line wasn't working is because of a timing issue. When we are calling onPressOut, we are getting the opacity from the prop in line 202. However, when that method got executed, the opacity is still 1, instead of 0.1. If you try to wrap line 176 with a I think your approach works and is reasonable. The downside is to trigger additional render by doing that in the |
@ryancat putting props.styles.opacity in useeffect will also cause rerender right!? and also what happened when you tried using getDerviedStateFromProps |
@ryancat Can you pleaser review |
@mikehardy @dulmandakh Can you please review?? |
Sorry @hetanthakkar1 - this is way out of the areas of anything I have knowledge of |
I have no experience with its implementation. Pressable is the future, and I don't use Touchable*. |
@ryancat can you please review |
@ryancat Can you please review |
ee17d78
to
949086d
Compare
Base commit: 48f6967 |
Closing in favor of #32956 |
Summary
This PR fixes the opacity bug where it fails to properly react to state change. This PR resolves the issue detailed in #32476
Changelog
[General] [Fixed] - Fixed opacity value in TouchableOpacity
Test Plan
The code I added in componentDidUpdate does solve the issue and passes all the test cases