Skip to content

fix: add ref forwarding support to styled()#221

Closed
tyrauber wants to merge 2 commits intonativewind:mainfrom
tyrauber:fix/styled-ref-forwarding
Closed

fix: add ref forwarding support to styled()#221
tyrauber wants to merge 2 commits intonativewind:mainfrom
tyrauber:fix/styled-ref-forwarding

Conversation

@tyrauber
Copy link
Copy Markdown
Contributor

  • fix ref forwarding issues preventing SVG and native components from receiving props
  • maintain full backward compatibility with existing styled() configurations
  • add test coverage for ref forwarding

Fixes issue where styled() didn't work with react-native-svg and expo-image components due to missing ref forwarding.

Users can now manually configure nativeStyleMapping for native components and it will work correctly.

- fix ref forwarding issues preventing SVG and native components
  from receiving props
- maintain full backward compatibility with existing styled()
  configurations
- add test coverage for ref forwarding

Fixes issue where styled() didn't work with react-native-svg and
expo-image components due to missing ref forwarding.

Users can now manually configure nativeStyleMapping for native
components and it will work correctly.
Copilot AI review requested due to automatic review settings October 22, 2025 21:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes ref forwarding issues in the styled() function that prevented proper integration with components like react-native-svg and expo-image. The fix ensures refs are properly forwarded through the styled component wrapper while maintaining backward compatibility.

Key changes:

  • Wrapped base components with forwardRef to enable ref forwarding
  • Updated both passThrough and non-passThrough code paths to handle refs correctly
  • Added comprehensive test coverage for ref forwarding scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/native/api.tsx Implemented ref forwarding by wrapping components with forwardRef and passing refs through the component hierarchy
src/tests/native/styled-ref-forwarding.test.tsx Added tests verifying ref forwarding works correctly with explicit nativeStyleMapping and regular components

Comment thread src/native/api.tsx Outdated
Comment thread src/native/api.tsx Outdated
Comment thread src/native/api.tsx Outdated
Remove redundant RefForwardedBase wrapper that caused duplicate
ref handling, as identified by GitHub Copilot.

Refs are now passed cleanly via { ref, ...props } directly to
usePassthrough/useNativeCss, eliminating unnecessary complexity
while maintaining full ref forwarding functionality.
@tyrauber tyrauber marked this pull request as draft October 22, 2025 23:22
@tyrauber
Copy link
Copy Markdown
Contributor Author

Actually, this might not resolve the issue.

It seems like only the global assignment fails for native component props, direct assignment works.

const StyledImage = styled(Image);

export default () => {
  return (
    <View className="flex-1 justify-center items-center">
      <StyledImage
        className="w-[200px] h-[200px]"
        source={require('@/assets/images/whatever.png')}
        contentFit="cover"
        transition={1000}
      />
    </View>
  );
};

The above works with react-native-css 3.0.0. It is only the global assignment as detailed in the nativewind v5 documentation that fails for native prop components.

Marking as draft, until I can debug further. The above is a work around for expo-image and svgs.

@tyrauber tyrauber closed this Oct 27, 2025
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.

2 participants