-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
Migrate away from setNativeProps
on web
#4352
Conversation
Thank you for this @tomekzaw |
…-set-native-props
thx @tomekzaw , is there an easy way to test this in moti? would love to get this to work in nextjs! |
If you use Then |
@tomekzaw Thank you! EDITED:
|
component.setNativeProps({ style: currentStyle }); | ||
|
||
const domStyle = createReactDOMStyle(currentStyle); | ||
for (const key in domStyle) { |
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.
We should handle this situation specifically because react-native-web
has already removed array transform. You can check it out here.
Something just like this:
import { createTransformValue } from 'react-native-web/dist/exports/StyleSheet/preprocess';
for (const key in domStyle) {
if(key === 'transform') {
(component.style).transform = createTransformValue(domStyle[key]);
}else{
(component.style)[key] = domStyle[key];
}
}
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.
does the current RNW function not account for it?
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.
Uunfortunately no. If you don't use createTransformValue
, useAnimatedStyle
will not work.
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 overall looks great 😊
I just posted some questions just to be sure.
Thanks @tomekzaw for the efforts. |
## Summary Smol fix after #4352 because `react-native-web` may not be installed. ## Test plan <!-- Provide a minimal but complete code snippet that can be used to test out this change along with instructions how to run it and a description of the expected behavior. -->
## Summary This draft PR moves off of `setNativeProps` in favor of editing DOM nodes directly. Closes software-mansion#4335, related to software-mansion#2756. ## Test plan <details> <summary>App.tsx</summary> ```tsx import Animated, { useSharedValue, withTiming, useAnimatedStyle, cancelAnimation, withRepeat, interpolateColor, } from 'react-native-reanimated'; import { View, StyleSheet } from 'react-native'; import React, { useEffect } from 'react'; export default function AnimatedStyleUpdateExample(): React.ReactElement { const randomWidth = useSharedValue(0); useEffect(() => { cancelAnimation(randomWidth); randomWidth.value = withRepeat(withTiming(1), -1, true); }, [randomWidth]); const style = useAnimatedStyle(() => { return { width: randomWidth.value * 100, height: randomWidth.value * 100, backgroundColor: interpolateColor( randomWidth.value, [0, 1], ['red', 'lime'] ), transform: [{ rotate: `${randomWidth.value * 180}deg` }], }; }); return ( <View style={styles.container}> <Animated.View style={style} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', }, }); ``` </details> https://github.com/software-mansion/react-native-reanimated/assets/20516055/93b8096f-2a4d-4c61-848d-f483d612ac13
## Summary Smol fix after software-mansion#4352 because `react-native-web` may not be installed. ## Test plan <!-- Provide a minimal but complete code snippet that can be used to test out this change along with instructions how to run it and a description of the expected behavior. -->
import createReactDOMStyle from 'react-native-web/dist/exports/StyleSheet/compiler/createReactDOMStyle'; | ||
|
||
let createTransformValue: (transform: any) => any; | ||
try { |
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 think it would be better to require this in the conditional itself, rather than at the root. I'm not sure if try catch
requires work with Webpack and most parsers.
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.
Yep, you're right, already done in #4473.
Summary
This draft PR moves off of
setNativeProps
in favor of editing DOM nodes directly.Closes #4335, related to #2756.
Test plan
App.tsx
Nagranie.z.ekranu.2023-05-15.o.13.45.19.mov