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

Question: How does this work? #27

Open
mfbx9da4 opened this issue Jan 11, 2022 · 2 comments
Open

Question: How does this work? #27

mfbx9da4 opened this issue Jan 11, 2022 · 2 comments

Comments

@mfbx9da4
Copy link

Hey,

I'm wondering if I can customize some underlying react-native components such as Image or Text by using an approach similar to this library. Ideally I would like to do something like

import { Image } from 'react-native'

export const setCustomImage = customProps => {
  const ImageRender = Image.render
  const initialDefaultProps = Image.defaultProps
  Image.defaultProps = {
    ...initialDefaultProps,
    ...customProps
  }
  Image.render = function render(props) {
    // write custom hooks and logic like any other component 👇
    const myCustomContext = useContext(CustomContext)
    let oldProps = props
    props = { ...props, style: [customProps.style, props.style] }
    try {
      return ImageRender.apply(this, arguments)
    } finally {
      props = oldProps
    }
  }
}

I'm pretty sure this won't work. I've never defined a react component with a .render method so is this something coming from the way the react native components work? I'm pretty sure this .render override strategy doesn't work with normal react components, so how does this work?

@dioi2000
Copy link

every(I haven't check it but 99% sure) RN components wrapped with forwardRef and it return elementType which have .render
if you want use same strategy, just wrap your component with forwardRef

var elementType = {
$$typeof: REACT_FORWARD_REF_TYPE,
render: render
};
forwardRef = (render) => elementType;

@mfbx9da4
Copy link
Author

ah makes sense, forgot about forwardRef!

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

No branches or pull requests

2 participants