Categories
JavaScript Answers React Native Answers

How to wrap React Native text on the screen?

Sometimes, we want to wrap React Native text on the screen.

In this article, we’ll look at how to wrap React Native text on the screen.

How to wrap React Native text on the screen?

To wrap React Native text on the screen, we can set flexWrap to 'wrap'.

For instance, we write:

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

const MyComponent = () => {
  return (
    <View style={{ flexDirection: 'row' }}>
      <Text style={{ flex: 1, flexWrap: 'wrap' }}>
        <View>
          <Text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</Text>
        </View>
        <View>
          <Text>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</Text>
        </View>
      </Text>
    </View>
  );
};

export default MyComponent;

We have a Views inside the Text component.

And we set the style prop of Text to an object with the flex property set to1 and flexWrap set to 'wrap'.

We set the View‘s flexDirection style to 'row' to enable horizontal flexbox.

Conclusion

To wrap React Native text on the screen, we can set flexWrap to 'wrap'.

Categories
JavaScript Answers React Native Answers

How to set 100% width in React Native flexbox?

Sometimes, we want to set 100% width in React Native flexbox.

In this article, we’ll look at how to set 100% width in React Native flexbox.

How to set 100% width in React Native flexbox?

To set 100% width in React Native flexbox, we can set alignSelf to 'stretch'.

For instance, we write:

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

const MyComponent = () => {
  return (
    <View>
      <Text
        style={{
          backgroundColor: 'yellow',
          alignSelf: 'stretch',
          textAlign: 'center',
        }}>
        hello world
      </Text>
    </View>
  );
};

export default MyComponent;

to set the alignSelf style to 'stretch' to make the Text component span the width of the screen.

Conclusion

To set 100% width in React Native flexbox, we can set alignSelf to 'stretch'.

Categories
JavaScript Answers React Native Answers

How to select the next TextInput after pressing the “next” keyboard button with React Native?

Sometimes, we want to select the next TextInput after pressing the "next" keyboard button with React Native.

In this article, we’ll look at how to select the next TextInput after pressing the "next" keyboard button with React Native.

How to select the next TextInput after pressing the "next" keyboard button with React Native?

To select the next TextInput after pressing the "next" keyboard button with React Native, we can assign refs to the TextInputs we want to set focus on.

And then we call focus on the ref.current of the TextInput to set focus on the input.

For instance, we write:

import * as React from 'react';
import { View, StyleSheet } from 'react-native';
import { TextInput } from 'react-native-paper';

const MyComponent = () => {
  const refInput2 = React.useRef();
  const refInput3 = React.useRef();

  return (
    <View>
      <TextInput
        placeholder="Input1"
        autoFocus={true}
        returnKeyType="next"
        onSubmitEditing={() => refInput2.current.focus()}
      />
      <TextInput
        placeholder="Input2"
        returnKeyType="next"
        onSubmitEditing={() => refInput3.current.focus()}
        ref={refInput2}
      />
      <TextInput placeholder="Input3" ref={refInput3} />
    </View>
  );
};

export default MyComponent;

to create 2 refs with useRef.

Then we set the ref prop of the 2nd and 3rd inputs.

Next, we set onSubmitEditing prop of the first 2 inputs to functions that calls focus on the inputs to put focus on them.

Therefore, when we press enter on the first input, focus is moved to the 2nd one.

And when we press enter on the 2nd input, focus is moved to the 3rd one.

Conclusion

To select the next TextInput after pressing the "next" keyboard button with React Native, we can assign refs to the TextInputs we want to set focus on.

And then we call focus on the ref.current of the TextInput to set focus on the input.

Categories
JavaScript Answers React Native Answers

How to add logging in React Native?

Sometimes, we want to add logging in React Native.

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

How to add logging in React Native?

To add logging in React Native, we can use console.

For instance, we write:

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

export default class MyComponent extends React.Component {
  render() {
    console.log('hello');
    console.warn('warning');

    return <View></View>;
  }
}

to call console.log to log messages and console.warn to log warnings.

Conclusion

To add logging in React Native, we can use console.

Categories
JavaScript Answers React Native Answers

How to insert a line break into a Text component in React Native?

Sometimes, we want to insert a line break into a Text component in React Native.

In this article, we’ll look at how to insert a line break into a Text component in React Native.

How to insert a line break into a Text component in React Native?

To insert a line break into a Text component in React Native, we can add the '\n' character string.

For instance, we write:

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

export default class MyComponent extends React.Component {
  render() {
    return (
      <View>
        <Text>
          Hi{'\n'}
          This is a test message.
        </Text>
      </View>
    );
  }
}

to add {'\n'} into the content of Text.

Then we see:

Hi
This is a test message.

rendered.

Conclusion

To insert a line break into a Text component in React Native, we can add the '\n' character string.