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

Value of opacity in TouchableOpacity properly react to state change #32477

Closed
wants to merge 1 commit into from

Conversation

hetanthakkar
Copy link
Contributor

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

@facebook-github-bot
Copy link
Contributor

Hi @hetanthakkar1!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@hetanthakkar hetanthakkar changed the title Solved: Value of opacity in TouchableOpacity properly react to state change Value of opacity in TouchableOpacity properly react to state change Oct 26, 2021
@hetanthakkar
Copy link
Contributor Author

@fkgozali @philIip Can you please review!?

@pull-bot
Copy link

PR build artifact for 0ce0496 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@analysis-bot
Copy link

analysis-bot commented Oct 26, 2021

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,297,341 -49
android hermes armeabi-v7a 7,634,241 -49
android hermes x86 8,770,678 -61
android hermes x86_64 8,707,885 -49
android jsc arm64-v8a 9,783,288 +3
android jsc armeabi-v7a 8,768,386 +7
android jsc x86 9,749,221 -11
android jsc x86_64 10,345,305 -10

Base commit: 48f6967
Branch: main

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 26, 2021
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 26, 2021
@pull-bot
Copy link

PR build artifact for 9f323c4 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

Copy link

@analysis-bot analysis-bot left a 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. Run yarn lint --fix to automatically fix problems.

Libraries/Components/Touchable/TouchableOpacity.js Outdated Show resolved Hide resolved
@pull-bot
Copy link

PR build artifact for bb78149 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@hetanthakkar
Copy link
Contributor Author

hetanthakkar commented Oct 26, 2021

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

facebook/flow#8509

@hetanthakkar
Copy link
Contributor Author

I don't know why the test failed can someone please help me!

@hetanthakkar
Copy link
Contributor Author

@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()),
Copy link
Contributor

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?

Copy link
Contributor

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.

@ryancat
Copy link
Contributor

ryancat commented Oct 30, 2021

I don't know why the test failed can someone please help me!

I don't think the failures are related to your changes. Could you rebase and try again?

@hetanthakkar
Copy link
Contributor Author

@ryancat Sure! Thanks

@hetanthakkar
Copy link
Contributor Author

@ryancat I'm trying to figure out why opacity didn't set on line 176

Copy link

@analysis-bot analysis-bot left a 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. Run yarn lint --fix to automatically fix problems.

@hetanthakkar
Copy link
Contributor Author

hetanthakkar commented Oct 31, 2021

@ryancat Actually on line 176 we tried to set the opacity in onPressOut but I don't think that we should set the opacity on press out because according to the docs: onPressOut will undo visual feedback which is not what we need! We need to provide visual feedback which can be done through onPressIn.

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.

@pull-bot
Copy link

PR build artifact for ee17d78 is ready.
To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

@hetanthakkar
Copy link
Contributor Author

Can someone please review my pull request

@ryancat
Copy link
Contributor

ryancat commented Nov 4, 2021

@ryancat Actually on line 176 we tried to set the opacity in onPressOut but I don't think that we should set the opacity on press out because according to the docs: onPressOut will undo visual feedback which is not what we need! We need to provide visual feedback which can be done through onPressIn.

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.

Thanks for the investigation! I am a bit confused though -- if we documented that onPressOut will undo visual feedback, isn't that shows we should do what we are doing now by trying to set the opacity to inactive state.

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 setTimeout and zero timeout, you will see this component working properly.

I think your approach works and is reasonable. The downside is to trigger additional render by doing that in the componentDidUpdate method. If you could avoid that additional render it would be great! I tried with getDerivedStateFromProps but not get very far. Using a functional approach would be better with more control on what dependencies we add to the opacity state.

@hetanthakkar
Copy link
Contributor Author

@ryancat putting props.styles.opacity in useeffect will also cause rerender right!? and also what happened when you tried using getDerviedStateFromProps

@hetanthakkar
Copy link
Contributor Author

@ryancat Can you pleaser review

@hetanthakkar
Copy link
Contributor Author

hetanthakkar commented Nov 13, 2021

@mikehardy @dulmandakh Can you please review??

@mikehardy
Copy link
Contributor

Sorry @hetanthakkar1 - this is way out of the areas of anything I have knowledge of

@dulmandakh
Copy link
Contributor

dulmandakh commented Nov 13, 2021

I have no experience with its implementation. Pressable is the future, and I don't use Touchable*.

@hetanthakkar
Copy link
Contributor Author

@ryancat can you please review

@hetanthakkar
Copy link
Contributor Author

@ryancat Can you please review

@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 48f6967
Branch: main

@cortinico
Copy link
Contributor

Closing in favor of #32956

@cortinico cortinico closed this Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants