Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?

Tags:

The documentation provided an example for StyleSheet.absoluteFillObject() whose behavior is also same while using with StyleSheet.absoluteFill():

const styles = StyleSheet.create({
  wrapper: {
    ...StyleSheet.absoluteFillObject,
    top: 10,
    backgroundColor: 'transparent',
  },
});

What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()? A little example will be more appreciated. Thanks !!!

like image 823
Purple Bytes Avatar asked Dec 07 '18 20:12

Purple Bytes


People also ask

Which method is used to create StyleSheet in React Native?

There are two main methods that React Native has for creating styles: inline styling and using a style prop to style subcomponents. Essentially, you have a StyleSheet. create method to define multiple styles at once, like an inline stylesheet in CSS.

What is StyleSheet in React Native?

React Native StyleSheet is a way of styling an application using JavaScript code, the main function of react native StyleSheet is concerned with styling and structuring of components in an application, all components of react native make use of a prop known as style, names and properties values work in a similar way as ...

Why would you use const styles StyleSheet create instead of a regular JavaScript object const styles?

Performance: Making a stylesheet from a style object makes it possible to refer to it by ID instead of creating a new style object every time. It also allows to send the style only once through the bridge.

How does react native’s stylesheet work?

Looking into the docs and reading a helpful article on Medium, it turns out React Native’s StyleSheet object gives us a few helpful methods to solve common styling needs: This causes the element to cover the entire screen, and sets the position property to “absolute”. A great example of when this is often useful is a modal overlay background.

What happens if a style is false in stylesheet?

If either style is falsy, the other one is returned without allocating an array, saving allocations and maintaining reference equality for PureComponent checks. Creates a StyleSheet style reference from the given object. Flattens an array of style objects, into one aggregated style object.

What is the difference between absolutefill and absolutefillobject?

If you want, absoluteFill can be used to create a customized entry in a StyleSheet, e.g.: Sometimes you may want absoluteFill but with a couple tweaks - absoluteFillObject can be used to create a customized entry in a StyleSheet, e.g.: This is defined as the width of a thin line on the platform.

How do I resolve an array of styles in stylesheet?

This method internally uses StyleSheetRegistry.getStyleByID (style) to resolve style objects represented by IDs. Thus, an array of style objects (instances of StyleSheet.create () ), are individually resolved to, their respective objects, merged as one and then returned. This also explains the alternative use.


1 Answers

absoluteFill is an easy way to set a view to be full screen and absolute positioned. It’s a short cut for:

{
 position: 'absolute',
 top: 0,
 left: 0,
 bottom: 0,
 right: 0

}

Use it to extend your other styles like this:

const styles = StyleSheet.create({
  container: {
   backgroundColor: 'red'
   }
});
<View style={[StyleSheet.absoluteFill, styles.container]} />

absoluteFillObject Say you want to absolute position your view, but bump it down 20px to offset for the status bar (for example). You can spread StyleSheet.absoluteFillObject into your style and then override one of it’s values.

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
   top: 20,
    backgroundColor: 'red'
  }
});
<View style={styles.container} />
like image 136
Charles Owen Avatar answered Sep 21 '22 11:09

Charles Owen