Skip to content

Instantly share code, notes, and snippets.

@anujraghuvanshi
Created April 8, 2021 19:43
Show Gist options
  • Save anujraghuvanshi/5f60a0663ee4d5b720c5514b070cdcd1 to your computer and use it in GitHub Desktop.
Save anujraghuvanshi/5f60a0663ee4d5b720c5514b070cdcd1 to your computer and use it in GitHub Desktop.
- Ability to modify the behaviour of default props of RN components (Here in this gist, increased font sized globally by 3px)
const setDefaultConfig = () => {
let components = [Text] // Components to modify.
const customProps = { style: {} }
const TextRender = components[0].render
const initialDefaultProps = components[0].constructor.defaultProps
components[0].render = function render(props) {
let oldProps = { ...props }
if (Array.isArray(oldProps.style)) {
oldProps.style = oldProps.style.reduce((obj, item) => Object.assign(obj, { ...item }), {});
}
if (oldProps.style && oldProps.style.fontSize && isTablet()) { // isTablet is custom function.
customProps.style.fontSize = oldProps.style.fontSize + 3
}
components[0].constructor.defaultProps = {
...initialDefaultProps,
...customProps
}
props = { ...props, style: { ...oldProps.style } }
if (Object.keys(customProps.style).length > 0) {
props.style = { ...JSON.parse(JSON.stringify(props.style)), ...customProps.style }
}
try {
return TextRender.apply(this, arguments)
} finally { }
}
}
// Set Configs.
setDefaultConfig()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment