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

7

24

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 !!!

Amnesty answered 7/12, 2018 at 20:57 Comment(0)
S
29

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} />
Solleret answered 7/12, 2018 at 22:54 Comment(2)
The thing you mention in the answer for StyleSheet.absoluteFillObject can also be done with the StyleSheet.absoluteFill, so, what can be the difference?Amnesty
According to this answer https://mcmap.net/q/544001/-react-native-what-is-the-difference-between-stylesheet-absolutefill-and-stylesheet-absolutefillobject it makes a difference when using TypeScript as you can't spread StyleSheet.absoluteFill into a stylesheet, apparentlyPhonology
A
15

There is no difference between those two. You can see this in StyleSheet.js:

 /**
   * A very common pattern is to create overlays with position absolute and zero positioning,
   * so `absoluteFill` can be used for convenience and to reduce duplication of these repeated
   * styles.
   */
  absoluteFill: (absoluteFill: any), // TODO: This should be updated after we fix downstream Flow sites.

  /**
   * Sometimes you may want `absoluteFill` but with a couple tweaks - `absoluteFillObject` can be
   * used to create a customized entry in a `StyleSheet`, e.g.:
   *
   *   const styles = StyleSheet.create({
   *     wrapper: {
   *       ...StyleSheet.absoluteFillObject,
   *       top: 10,
   *       backgroundColor: 'transparent',
   *     },
   *   });
   */
  absoluteFillObject: absoluteFill,

enter image description here

Augusto answered 27/2, 2019 at 3:39 Comment(1)
Yes, "Use the Source, Luke"! Here's the reference to check what is the situation in the latest version: github.com/facebook/react-native/blob/master/Libraries/…Asyndeton
G
8

I may be late for the party. But there is some difference between absoluteFill and absoluteFillObject in typescript.

Mainly in typescript, the type of:

  • absoluteFill is RegisteredStyle<StyleSheet.AbsoluteFillStyle>
  • absoluteFillObject is StyleSheet.AbsoluteFillStyle
const styles = StyleSheet.create({
    container: {
        // must use "absoluteFillObject" in typescript
        ...StyleSheet.absoluteFillObject,
    }
})

For JavaScript, there is no difference.

Galvan answered 15/11, 2021 at 0:22 Comment(0)
H
4

As of version 0.62, no difference at all according to the official document

In case you are using EXPO Snack like I do, the absoluteFill preview on web seems buggy at this time. On a real device, it should be fine.

Hynda answered 16/5, 2020 at 3:39 Comment(0)
M
2

Currently (React Native 0.66), the documentation states:

there is no difference between using absoluteFill vs. absoluteFillObject.

Macdermot answered 19/11, 2021 at 9:46 Comment(0)
E
1

I've tried to print the value of absoluteFill and absoluteFillObject.
They're no difference. They're the same value.

[LOG] absoluteFill: {"bottom": 0, "left": 0, "position": "absolute", "right": 0, "top": 0}
[LOG] absoluteFillObject: {"bottom": 0, "left": 0, "position": "absolute", "right": 0, "top": 0}
Eddie answered 22/4, 2021 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.