grapp-dev / stacks

A set of components for building layouts in React Native. Powered by React Native Unistyles.
https://stacks.grapp.dev
MIT License
985 stars 24 forks source link

[Box] forward ref of view #27

Closed mlecoq closed 3 years ago

mlecoq commented 3 years ago

Hi,

I'm using more and more your lib (and delete tons of styles), and I am often annoyed when I want to use a ref on a Box

I would like to measure it using https://reactnative.dev/docs/direct-manipulation#measurecallback

For the moment I wrap my Box with a view but I would like to measure the Box directly

What do you think ? Could we have a forward ref on Box ?

mobily commented 3 years ago

@mlecoq 👋 I think it's doable, but could you provide a simple snippet of your current approach? :)

mlecoq commented 3 years ago

@mobily, Sure !

Here it is


   <RemarkItemContainer first={first} ref={containerRef}>
        <Box padding={[10, 15]}>
        ....
        </Box>
   </RemarkItemContainer>

RemarkItemContainer is created with styles-components

const RemarkItemContainer = styled.View...

My goal is to be able to have

const RemarkItemContainer = styled(Box)...


   <RemarkItemContainer first={first} ref={containerRef} padding={[10, 15]}>
        ....
   </RemarkItemContainer>

And elsewhere

containerRef.current.measure....

mobily commented 3 years ago

@mlecoq 👋 sorry it took so loooong! I finally found spare time, the viewRef prop has been added to all components in v1.3.0. You can see the example here: https://github.com/mobily/stacks/blob/9a5deb65fbe73391988d7a4470bdae7ed26bafa8/example/src/typescript/screens/Playground/index.tsx#L23