Categories
JavaScript Answers React Native Answers

How to vertically align image with resizeMode “contain” with React Native?

Sometimes, we want to vertically align image with resizeMode "contain" with React Native.

In this article, we’ll look at how to vertically align image with resizeMode "contain" with React Native.

How to vertically align image with resizeMode "contain" with React Native?

To vertically align image with resizeMode "contain" with React Native, we can use justifyContent.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ justifyContent: 'center', flex: 1 }}>
      <Image
        source={{ uri: 'https://picsum.photos/200/300' }}
        style={{ height: 100, width: 300 }}
        resizeMode="contain"
        resizeMethod="resize"
      />
    </View>
  );
}

to set justifyContent to 'center' to center align the Image vertically.

We also set the image height and width to make it display.

As a result, we should see the image display vertically centered.

Conclusion

To vertically align image with resizeMode "contain" with React Native, we can use justifyContent.

Categories
JavaScript Answers React Native Answers

How to fix textAlign: ‘right’ not styling correctly with React Native?

Sometimes, we want to fix textAlign: ‘right’ not styling correctly with React Native.

In this article, we’ll look at how to fix textAlign: ‘right’ not styling correctly with React Native.

How to fix textAlign: ‘right’ not styling correctly with React Native?

To fix textAlign: ‘right’ not styling correctly with React Native, we can set textAlign to 'right' on the Text component in addition to using flexbox.

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={{ flex: 1, flexDirection: 'row' }}>
      <View style={{ flex: 1 }}>
        <Text>100 Views 0 Comments</Text>
      </View>
      <View style={{ flex: 1 }}>
        <Text style={{ textAlign: 'right' }}>View</Text>
      </View>
    </View>
  );
}

to set flexDirection to 'row' to let us display the inner views side by side.

Then we set textAlign to 'right' on the 2nd Text component to align the text of that on the right.

Conclusion

To fix textAlign: ‘right’ not styling correctly with React Native, we can set textAlign to 'right' on the Text component in addition to using flexbox.

Categories
JavaScript Answers React Native Answers

How to change the React Native text input cursor color?

Sometimes, we want to change the React Native text input cursor color.

In this article, we’ll look at how to change the React Native text input cursor color.

How to change the React Native text input cursor color?

To change the React Native text input cursor color, we can set the selectionColor prop to the cursor color.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <TextInput selectionColor="green" placeholder="name" />
    </View>
  );
}

to set selectionColor to 'green' to set the cursor color on the text input to green.

Conclusion

To change the React Native text input cursor color, we can set the selectionColor prop to the cursor color.

Categories
JavaScript Answers React Native Answers

How to call child component function from parent component in React Native?

Sometimes, we want to call child component function from parent component in React Native.

In this article, we’ll look at how to call child component function from parent component in React Native.

How to call child component function from parent component in React Native?

To call child component function from parent component in React Native, we can use the useImperativeHandle hook.

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';

const Child = React.forwardRef((props, ref) => {
  const { foo } = props;
  React.useImperativeHandle(ref, () => ({
    method: () => {
      console.log(foo);
    },
  }));

  return <View></View>;
});

export default function App() {
  const ref = React.useRef();
  const onPress = () => {
    ref.current.method();
  };

  return (
    <View style={{ flex: 1 }}>
      <Button onPress={onPress} title="Hello" />
      <Child foo="bar" ref={ref} />
    </View>
  );
}

to create the Child component with forWardRef with a function that takes the props and ref.

In it, we call useImperativeHandle with ref and a function that returns the methods we can call from the ref assigned to the Child component.

Next, we create a ref with useRef in App.

And then we call ref.current.method in onPress which is run when we press the button.

We add Child beneath Button and assign a ref to it to let us call ref.current.method.

Conclusion

To call child component function from parent component in React Native, we can use the useImperativeHandle hook.

Categories
JavaScript Answers React Native Answers

How to add a button in React Native?

Sometimes, we want to add a button in React Native.

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

How to add a button in React Native?

To add a button in React Native, we can use the Button component.

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() {
  const onPress = () => {
    console.log('button pressed');
  };

  return (
    <View style={{ flex: 1 }}>
      <Button onPress={onPress} title="Hello" color="green" />
    </View>
  );
}

to add a Button.

The title of the button is the button text.

color sets the background color.

The onPress prop is set to the onPress function which runs when the button is pressed.

As a result, 'button pressed' is logged when we press on the button.

Conclusion

To add a button in React Native, we can use the Button component.