Categories
JavaScript Answers React Native Answers

How to apply different styles applied on orientation change with React Native?

Sometimes, we want to apply different styles applied on orientation change with React Native.

In this article, we’ll look at how to apply different styles applied on orientation change with React Native.

How to apply different styles applied on orientation change with React Native?

To apply different styles applied on orientation change with React Native, we can call Dimension.addEventListener and the Platform.isPortrait methods.

For instance, we write:

import * as React from 'react';
import { View, Text, Dimensions, Platform } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  React.useEffect(() => {
    const onDimensionChange = () => {
      console.log(Platform.isPortrait());
    };

    Dimensions.addEventListener('change', onDimensionChange);
    return () => {
      Dimensions.removeEventListener('change', onDimensionChange);
    };
  }, []);
  
  return <View></View>;
}

to call Dimensions.addEventListener method with 'change' to watch for changes in the screen dimensions.

onDimensionChange is the screen dimension change listener.

Platform.isPortrait returns true is our screen is in portrait mode.

We return a function that calls Dimensions.removeEventListener to remove the listener when App unmounts.

Conclusion

To apply different styles applied on orientation change with React Native, we can call Dimension.addEventListener and the Platform.isPortrait methods.

Categories
JavaScript Answers React Native Answers

How to make items display inline in React Native?

Sometimes, we want to make items display inline in React Native.

In this article, we’ll look at how to make items display inline in React Native.

How to make items display inline in React Native?

To make items display inline in React Native, we can use set flexDirection to 'row'.

For instance, we write:

import * as React from 'react';
import { View, Text } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View
      style={{
        flexWrap: 'wrap',
        alignItems: 'flex-start',
        flexDirection: 'row',
      }}>
      <Text style={{ width: 100 }}>foo</Text>
      <Text style={{ width: 100 }}>bar</Text>
    </View>
  );
}

to set the outer view’s style to have flexDirection set to 'row' and flexWrap set to 'wrap' to make the Text components display side by side and wrap them to the next row if they overflow the width of the screen.

alignItems is set to 'flex-start' to make the align the Text components on the left on the screen.

Conclusion

To make items display inline in React Native, we can use set flexDirection to 'row'.

Categories
JavaScript Answers React Native Answers

How to set up a table layout in React Native?

Sometimes, we want to set up a table layout in React Native.

In this article, we’ll look at how to set up a table layout in React Native.

How to set up a table layout in React Native?

To set up a table layout in React Native, we can nest views.

For instance, we write:

import * as React from 'react';
import { View, Text } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

const renderRow = (item) => {
  return (
    <View
      style={{ flex: 1, alignSelf: 'stretch', flexDirection: 'row' }}
      key={item}>
      <View style={{ flex: 1, alignSelf: 'stretch' }}>
        <Text>{item}</Text>
      </View>
      <View style={{ flex: 1, alignSelf: 'stretch' }}>
        <Text>{item}</Text>
      </View>
      <View style={{ flex: 1, alignSelf: 'stretch' }}>
        <Text>{item}</Text>
      </View>
    </View>
  );
};

export default function App() {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      {Array(5)
        .fill()
        .map((_, i) => {
          return renderRow(i);
        })}
    </View>
  );
}

to create the renderRow function that renders a View with Views inside.

We make them display side by side by setting flexDirection to 'row'.

And we make the outer View span the width of the outermost View by setting alignSelf to 'stretch' and flex to 1.

Finally, we call renderRow in the map callback to render the nested Views in renderRow.

Conclusion

To set up a table layout in React Native, we can nest views.

Categories
JavaScript Answers React Native Answers

How to add a full width button with flex-box in React Native?

Sometimes, we want to add a full width button with flex-box in React Native.

In this article, we’ll look at how to add a full width button with flex-box in React Native.

How to add a full width button with flex-box in React Native?

To add a full width button with flex-box in React Native, we can set the alignSelf style to 'stretch'.

For instance, we write:

import * as React from 'react';
import { View, Button } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

export default function App() {
  return (
    <View>
      <Button title="button" style={{ alignSelf: 'stretch' }} />
    </View>
  );
}

to add a Button with the alignSelf style set to 'stretch' to make the button stretch to the width of the screen.

Conclusion

To add a full width button with flex-box in React Native, we can set the alignSelf style to 'stretch'.

Categories
JavaScript Answers React Native Answers

How to add a transparent overlay in React Native?

Sometimes, we want to add a transparent overlay in React Native.

In this article, we’ll look at how to add a transparent overlay in React Native.

How to add a transparent overlay in React Native?

To add a transparent overlay in React Native, we can use the ImageBackground component.

For instance, we write:

import * as React from 'react';
import { View, ImageBackground, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

const s = StyleSheet.create({
  backgroundImage: {
    flex: 1,
    width: null,
    height: null,
    backgroundColor: 'red',
    opacity: 0.3,
  },
});

export default function App() {
  return (
    <ImageBackground
      source={{ uri: 'https://picsum.photos/200/300' }}
      style={s.backgroundImage}>
      <View />
    </ImageBackground>
  );
}

to add an ImageBackground with the source set to an object with the uri of the background image.

And we add the backgroundImage style that sets opacity to 0.3 to add a transparent overlay over the image.

Conclusion

To add a transparent overlay in React Native, we can use the ImageBackground component.