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

How to center text with React Native?

Sometimes, we want to center text with React Native.

In this article, we’ll look at how to center text with React Native.

How to center text with React Native?

To center text with React Native, we can use flexbox-like styles.

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 style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>hello world</Text>
      </View>
    );
  }
}

to set flex to 1 to expand the View to fit the screen.

And we use justifyContent to 'center' to center horizontally and alignItems to center vertically.

Then we put our text in the Text component.

Therefore, ‘hello world’ is centered.

Conclusion

To center text with React Native, we can use flexbox-like styles.

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.

Categories
JavaScript Answers

How to get the filename from a file input with JavaScript?

Sometimes, we want to get the filename from a file input with JavaScript.

In this article, we’ll look at how to get the filename from a file input with JavaScript.

How to get the filename from a file input with JavaScript?

To get the filename from a file input with JavaScript, we can can it from the files property.

For instance, we write:

<input type='file'>

to add a file input.

Then we write:

const input = document.querySelector("input")
input.onchange = (e) => {
  const [file] = e.target.files
  console.log(file.name)
}

to select the input with querySelector.

Then we set input.onchange to a function that gets the selected files from e.target.files.

And then we get the file from the files object and get the file name from file.name.

Conclusion

To get the filename from a file input with JavaScript, we can can it from the files property.