React Native: Device Independent grid of squares with flexbox

react native grid
react-native flatlist grid
react native flexbox tutorial
dynamic grid view in react native
react native grid view
react native grid of buttons
flexwrap react native
react native-square

I'd like to display the same grid with squares independently if the user has an iPad or an iPhone, growing the square sizes accordingly.

I do not want to detect the device and change the width and height of the squares based on the screen size. Is it possible to use only the css flex capabilities of React Native?

Here the rnplay example


Well there is a trick which allows you to actually create a grid of squares using flexbox- maybe you can adopt it to React Native.

Example:

  1. First I created a flexbox with 3 boxes in a row that wraps into multiple rows:

    .flexbox {
      list-style: none;
      display: flex;
      flex-wrap: wrap;
    }
    .flexbox > * {
      margin: 5px;
      text-align: center;
      border: 1px solid red;
      flex-basis: calc(33.33% - 10px);
    }
    

    Note flex-basis: calc(33.33% - 10px) that allows to keep 3 boxes in a row- change this to accommodate more boxes. (10px here is the double of the margin).

  2. And here is the trick that makes them squares- make a pseudo inline-block element which has padding-bottom: 100%. (If you give padding/margin in percentages, then it is always taken with respect to the width)

    .flexbox > *:before {
      content: '';
      padding-bottom: 100%;
      height: 100%;
      display: inline-block;
      vertical-align: middle;
    }
    

Let me know your feedback on this. Thanks!

body {
  padding: 0;
  margin: 0;
}
* {
  box-sizing: border-box;
}
.flexbox {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
}
.flexbox > * {
  margin: 5px;
  text-align: center;
  border: 1px solid red;
  flex-basis: calc(33.33% - 10px);
}
.flexbox > *:before {
  content: '';
  padding-bottom: 100%;
  height: 100%;
  display: inline-block;
  vertical-align: middle;
}
<body>
  <div class="flexbox">
    <div>some text here</div>
    <div>some text here</div>
    <div>some text here</div>
    <div>some text here</div>
    <div>some text here</div>
    <div>some text here</div>
    <div>some text here</div>
    <div>some text here</div>
  </div>
</body>

In-Depth Styling with React Native and Flexbox - Manning , Flexbox is a layout implementation that React Native uses to provide an The React Native Flexbox implementation is based on the W3C and allows a child object to be aligned independently of its peers. The row of squares is chopped off at the right edge, and you can't see anything after that point. Flexbox is designed to provide a consistent layout on different screen sizes. You will normally use a combination of flexDirection, alignItems, and justifyContent to achieve the right layout. Flexbox works the same way in React Native as it does in CSS on the web, with a few exceptions.


The current (0.33.0 at time of writing) flexbox capabilities of React Native (using flexDirection and flex) can only give you even widths or heights, but not both at the same time. Why don't you want to use Dimensions to measure the screen size?

Get Started With React Native Layouts - Tuts+ Code, This includes the Stack Layout, Grid Layout, and Absolute Layout. We'll create a separate component for each layout that we'll implement Layouts in React Native use a subset of Flexbox. That's because as soon as the user flips their device, the width or height that you computed earlier will be wrong. Remember to take the device PixelRatio into account. When you work with pixel values in a UI, most of the time those units are "logical pixels" or "device-independent pixels".


Add

aspectRatio: 1

in the style of the component if a width or height is already set by say a flex: 1 and it will generate the other value for you!

RangerMauve/react-native-square-grid: A component for , A component for rendering a grid of squares that perfectly fill your space (​potentially with scrolling) - RangerMauve/react-native-square-grid. React Native uses Flexbox to position elements on the screen, and you'll notice that we set justifyContent: 'flex-end' on the image, which means that its children (in this case, the text component) will stack from the end (the bottom). Using the window's dimensions, we can set our images to a lovely full-width and a height that's slightly smaller than one-third of the screen.


Flexbox square image, Flexbox arranges DOM elements in rows or columns: Unlike CSS grid, Flexbox Views are the most basic building block of React Native apps, and the inner view The Display orOder of Flexbox Elements is Independent of Their The Flexible Box To Accommodate Different Screen Sizes and Different Display Devices. Our comprehensive guide to CSS flexbox layout. This complete guide explains everything about flexbox, focusing on all the different possible properties for the parent element (the flex container) and the child elements (the flex items). It also includes history, demos, patterns, and a browser support chart.


A Complete Guide to Flexbox, Flexbox layout (Flexible Box) hướng đến việc cung cấp một cách Note: Flexbox layout thích hợp cho việc bố trí các thành phần của một trong khi Grid thích hợp cho việc bố trí các thành phần quy mô lớn Article says it should be independent. <meta name="viewport" content="width=device-width">. What bugs me in the flexbox layout is that it is supposed to be used without changing the html. I'm by no means html, css expert - quite the opposite so please correct me if I'm wrong. You just mark one element as flexbox container and that's it. But in the native platforms (like Android and WU) elements cannot be added everywhere.


React dynamic layout, Using styled-components to achieve dynamic layouts in React Native Clean up your The Grid layout component provides a semi-automated, responsive grid layout If you still have to support IE9 and below, flexbox is off the table. use relative sizes together with device-independent offsets, and position elements on​  This default may be noticeable and undesired in the case where the View you are setting an opacity on has multiple overlapping elements (e.g. multiple overlapping Views, or text and a background). Rendering offscreen to preserve correct alpha behavior is extremely expensive and hard to debug for non-native developers, which is why it is not