Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6212

Monkey patch react-native Text component with TypeScript

$
0
0

I want to monkeypatch react-native Text element render function.

Typescript shows me some errors, as the exported Text class does not expose the defaultProps static member each instance has internally (via TouchableText implementation inside react-native) and the render function which is also not party of the public interface.

However the code below is valid JS code (at least) and it works correctly to achieve my goal.I'd still like to get rid of the TS warnings.

const setCustomText = (customProps: TextProps) => {  Text.defaultProps = {    ...Text.defaultProps,    ...customProps,  }  const orgRender = Text.render  Text.render = function render(props:TextProps, ref:Ref<Text>) {    const style = Array.isArray(props.style) ? props.style : [props.style]    props = {      ...props,      style: [Text.defaultProps.style, ...style],    }    return orgRender.call(this, props, ref)  }}

It complains:

Text.defaultProps: Property 'defaultProps' does not exist on type 'typeof Text'.ts(2339)Text.render: Property 'render' does not exist on type 'typeof Text'.ts(2339)


Viewing all articles
Browse latest Browse all 6212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>