Categories
JavaScript Answers React Native Answers

How to identify return key press in React Native?

Sometimes, we want to identify return key press in React Native.

In this article, we’ll look at how to identify return key press in React Native.

How to identify return key press in React Native?

To identify return key press in React Native, we can set the onKeyPress prop to a function to listen for which key is pressed.

For instance, we write:

import * as React from 'react';
import { View, TextInput } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
import dismissKeyboard from 'react-native-dismiss-keyboard';

export default function App() {
  const handleKeyDown = (e) => {
    if (e.nativeEvent.key === 'Enter') {
      dismissKeyboard();
    }
  };

  return (
    <View style={{ padding: 30 }}>
      <TextInput
        autoCapitalize="sentences"
        autoCorrect
        keyboardType="default"
        returnKeyType="done"
        onKeyPress={handleKeyDown}
        placeholder="Enter text here..."
      />
    </View>
  );
}

to define the handleKeyDown function that get the key pressed with e.nativeEvent.key.

And it’s equal to 'Enter', then we call dismissKeyboard to remove the keyboard from the screen.

We then set onKeyPress to handleKeyDown to make handleKeyDown the event listener for the key press event.

Conclusion

To identify return key press in React Native, we can set the onKeyPress prop to a function to listen for which key is pressed.

Categories
JavaScript Answers React Native Answers

How to render a shadow with React Native?

Sometimes, we want to render a shadow with React Native.

In this article, we’ll look at how to render a shadow with React Native.

How to render a shadow with React Native?

To render a shadow with React Native, we can set some shadow styles.

For instance, we write:

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

export default function App() {
  return (
    <View
      style={{
        padding: 10,
        margin: 10,
        shadowColor: '#000',
        shadowOffset: { width: 3, height: 2 },
        shadowOpacity: 0.2,
        elevation: 5,
      }}>
      <Text>hello world</Text>
    </View>
  );
}

to set the shadowColor, shadowOffset and shadowOpacity to add a shadow to the View.

shadowOffset adds the shadow effect.

shadowColor sets the shadow color.

And shadowOpacity sets the shadow opacity.

Conclusion

To render a shadow with React Native, we can set some shadow styles.

Categories
JavaScript Answers React Native Answers

How to format a date string in React Native?

To format a date string in React Native, we can use moment.js.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <Text>{moment(new Date(2022, 1, 1)).format('YYYY-MM-DD')}</Text>
    </View>
  );
}

to call moment with a JavaScript date object to create a moment object with the same date.

Then we call format with a format string to return a date string with the given date.

Therefore, we see 2022-02-01 displayed.

Categories
JavaScript Answers React Native Answers

How to fit Image in containing View with React Native?

Sometimes, we want to fit Image in containing View with React Native.

In this article, we’ll look at how to fit Image in containing View with React Native.

How to fit Image in containing View with React Native?

To fit Image in containing View with React Native, we can set the width of the Image to '100%'.

For instance, we write:

import * as React from 'react';
import { View, Image } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
import { WebView } from 'react-native-webview';

export default function App() {
  const [text, setText] = React.useState();
  return (
    <View style={{ width: 200, height: 300 }}>
      <Image
        source="https://picsum.photos/200/300"
        style={{
          width: '100%',
          height: undefined,
          aspectRatio: 1,
        }}
      />
    </View>
  );
}

to set the style prop of the Image to

{
  width: '100%',
  height: undefined,
  aspectRatio: 1,
}

to make the width of the image fit to the View‘s width.

And we set the aspectRatio to 1 to make the height the same as the width.

Conclusion

To fit Image in containing View with React Native, we can set the width of the Image to '100%'.

Categories
JavaScript Answers React Native Answers

How to add a text area in React Native?

Sometimes, we want to add a text area in React Native.

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

How to add a text area in React Native?

To add a text area in React Native, we can use a TextInput with the multiline and the numberOfLines props.

For instance, we write:

import * as React from 'react';
import { TextInput, View, Text } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
import { WebView } from 'react-native-webview';

export default function App() {
  const [text, setText] = React.useState();

  return (
    <View style={{ flex: 1 }}>
      <TextInput
        multiline
        numberOfLines={4}
        onChangeText={(text) => setText(text)}
        value={text}
      />
      <Text>{text}</Text>
    </View>
  );
}

to add a TextInput with the multiline prop to add a text area.

Then we set numberOfLines to 4 to make the text input line 4 lines tall.

We set the onChangeText prop to a function that sets the text state to the input value.

And we set the value prop to text to show the input value.

Conclusion

To add a text area in React Native, we can use a TextInput with the multiline and the numberOfLines props.