Giter Site home page Giter Site logo

lucasvnborges / react-native-style-utilities Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrousavy/react-native-style-utilities

0.0 0.0 0.0 247 KB

Fully typed hooks and utility functions for the React Native StyleSheet API

Home Page: https://gist.github.com/mrousavy/0de7486814c655de8a110df5cef74ddc

License: MIT License

JavaScript 51.12% TypeScript 48.88%

react-native-style-utilities's Introduction

react-native-style-utilities

Fully typed hooks and utility functions for the React Native StyleSheet API


npm i react-native-style-utilities

ESLint Setup

If you're using the eslint-plugin-react-hooks plugin, add the following to your .eslintrc.js:

"react-hooks/exhaustive-deps": [
  "error",
  {
    additionalHooks: "(useStyle|useFlatStyle)",
  },
],


useStyle

A hook to memoize dynamic styles.

See "Memoize!!! ๐Ÿ’พ - a react (native) performance guide"

Objects

By using useStyle the object { height: number } gets memoized and will only be re-created if someDynamicValue changes, resulting in better optimized re-renders.

Bad

return <View style={{ height: someDynamicValue }} />

Good

const style = useStyle(() => ({ height: someDynamicValue }), [someDynamicValue])

return <View style={style} />

Arrays

useStyle can also be used to join arrays together, also improving re-render times.

Bad

return <View style={[styles.container, props.style, { height: someDynamicValue }]} />

Good

const style = useStyle(
  () => [styles.container, props.style, { height: someDynamicValue }],
  [props.style, someDynamicValue]
);

return <View style={style} />


useFlatStyle

Same as useStyle, but flattens ("merges") the returned values into a simple object with StyleSheet.flatten(...).

See "Memoize!!! ๐Ÿ’พ - a react (native) performance guide"

const style1 = useStyle(
  () => [styles.container, props.style, { height: someDynamicValue }],
  [props.style, someDynamicValue]
);
style1.borderRadius // <-- does not work, `style1` is an array!

const style2 = useFlatStyle(
  () => [styles.container, props.style, { height: someDynamicValue }],
  [props.style, someDynamicValue]
);
style2.borderRadius // <-- works, will return 'number | undefined'


findStyle

A helper function to find a given style property in any style object without using expensive flattening (no StyleSheet.flatten(...)).

function Component({ style, ...props }) {
  const borderRadius = style.borderRadius // <-- does not work, style type is complex
  const borderRadius = findStyle(style, "borderRadius") // <-- works, is 'number | undefined'
}

react-native-style-utilities's People

Contributors

mrousavy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.