Categories
JavaScript Answers React Native Answers

How to draw a horizontal rule in React Native?

Sometimes, we want to draw a horizontal rule in React Native.

In this article, we’ll look at how to draw a horizontal rule in React Native.

How to draw a horizontal rule in React Native?

To draw a horizontal rule in React Native, we can add a View with a border color and width.

For instance, we write:

import * as React from 'react';
import { Text, View } from 'react-native';

import { Card } from 'react-native-paper';

const App = () => {
  return (
    <View style={{ flex: 1 }}>
      <Text>hello</Text>
      <View
        style={{
          borderBottomColor: 'black',
          borderBottomWidth: 1,
        }}
      />
      <Text>world</Text>
    </View>
  );
};
export default App;

to add the View between 2 Text components.

We set the borderBottomColor to 'black' to add a border color.

And then we set borderBottomWidth to 1 to add a border width in pixels.

Conclusion

To draw a horizontal rule in React Native, we can add a View with a border color and width.

Categories
JavaScript Answers React Native Answers

How to make dynamic styles in React Native?

Sometimes, we want to make dynamic styles in React Native.

In this article, we’ll look at how to make dynamic styles in React Native.

How to make dynamic styles in React Native?

To make dynamic styles in React Native, we can return style objects from functions.

For instance, we write:

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

import { Card } from 'react-native-paper';

const createStyles = (background) => {
  return {
    borderRadius: 12,
    background,
  };
};

const App = () => {
  return (
    <View style={{ flex: 1, ...createStyles('yellow') }}>
      <Text>hello world</Text>
    </View>
  );
};
export default App;

to create the createStyles function that returns an object with some style properties.

Then we spread the returned style object into the object we set as the value of the style prop of the View.

Conclusion

To make dynamic styles in React Native, we can return style objects from functions.

Categories
JavaScript Answers React Native Answers

How to style a TextInput in React Native for password input?

Sometimes, we want to style a TextInput in React Native for password input

In this article, we’ll look at how to style a TextInput in React Native for password input.

How to style a TextInput in React Native for password input?

To style a TextInput in React Native for password input, we can set the secureTextEntry prop to true.

For instance, we write:

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

import { Card } from 'react-native-paper';

const App = () => {
  return (
    <View style={{ flex: 1 }}>
      <TextInput secureTextEntry />
    </View>
  );
};
export default App;

to add the secureTextEntry prop to the TextInput add a password input.

Conclusion

To style a TextInput in React Native for password input, we can set the secureTextEntry prop to true.

Categories
JavaScript Answers React Native Answers

How to add a fixed footer with React Native?

Sometimes, we want to add a fixed footer with React Native.

In this article, we’ll look at how to add a fixed footer with React Native.

How to add a fixed footer with React Native?

To add a fixed footer with React Native, we can add a ScrollView on top of the footer View.

For instance, we write:

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

import { Card } from 'react-native-paper';

const App = () => {
  return (
    <View style={{ flex: 1 }}>
      <ScrollView>main</ScrollView>
      <View>
        <Text>footer</Text>
      </View>
    </View>
  );
};
export default App;

to set the outer View to have the flex style set to 1 to make it fill the screen.

Then we add a ScrollView that fills the height of the outer View except for the height of the footer View.

Now we should see ‘footer’ stays at the bottom.

Conclusion

To add a fixed footer with React Native, we can add a ScrollView on top of the footer View.

Categories
JavaScript Answers React Native Answers

How to add a full screen background image with React Native?

Sometimes, we want to add a full screen background image with React Native.

In this article, we’ll look at how to add a full screen background image with React Native.

How to add a full screen background image with React Native?

To add a full screen background image with React Native, we can set the flex style to 1.

For instance, we write:

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

import { Card } from 'react-native-paper';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    width: null,
    height: null,
  },
});

const App = () => {
  return (
    <View style={styles.container}>
      <Image source={require('./assets/snack-icon.png')} style={styles.container}></Image>
    </View>
  );
};
export default App;

to add the styles.container styles object that sets flex to 1 on the View and Image so they both stretch to fit their parent container.

We add the image with the Image component.

The source has the image path.

As a result, the image should be full screen.

Conclusion

To add a full screen background image with React Native, we can set the flex style to 1.