Categories
JavaScript Answers React Native Answers

How to open a URL in default web browser with React Native?

Sometimes, we want to open a URL in default web browser with React Native.

In this article, we’ll look at how to open a URL in default web browser with React Native.

How to open a URL in default web browser with React Native?

To open a URL in default web browser with React Native, we can use the Linking.openURL method.

For instance, we write:

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

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

const App = () => {
  const onPress = async () => {
    const url = 'https://example.com';
    await Linking.canOpenURL(url);
    Linking.openURL(url);
  };

  return (
    <View>
      <Text onPress={onPress}>open</Text>
    </View>
  );
};
export default App;

to define the onPress function that calls Linking.canOpenURL to check if the url can be opened.

If it resolves to true, then we call Linking.openURL with the url to open the link in the default browser of the device.

Conclusion

To open a URL in default web browser with React Native, we can use the Linking.openURL method.

Categories
JavaScript Answers React Native Answers

How to get the screen width in React Native?

Sometimes, we want to get the screen width in React Native.

In this article, we’ll look at how to get the screen width in React Native.

How to get the screen width in React Native?

To get the screen width in React Native, we can use the Dimension object.

For instance, we write:

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

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

const App = () => {
  const dims = {
    width: Dimensions.get('window').width,
    height: Dimensions.get('window').height,
  };

  return (
    <View>
      <Text>{JSON.stringify(dims)}</Text>
    </View>
  );
};
export default App;

We call Dimensions.get with 'window' to get the screen dimensions in an object.

Then we can use the width and height properties to get the width and height of the screen.

Conclusion

To get the screen width in React Native, we can use the Dimension object.

Categories
JavaScript Answers React Native Answers

How to make text vertical (rotated 90 degrees) in React Native?

Sometimes, we want to make text vertical (rotated 90 degrees) in React Native.

In this article, we’ll look at how to make text vertical (rotated 90 degrees) in React Native.

How to make text vertical (rotated 90 degrees) in React Native?

To make text vertical (rotated 90 degrees) in React Native, we can set the transform style.

For instance, we write:

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

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

const App = () => {
  return (
    <View>
      <Text
        style={{
          transform: [{ rotate: '90deg' }],
          position: 'fixed',
          top: '30px',
        }}>
        hello world
      </Text>
    </View>
  );
};
export default App;

to set the transform style to [{ rotate: '90deg' }] to rotate the Text component by 90 degrees.

We also set the position to 'fixed' and top to '30px' to move the text.

Now we should see ‘hello world’ displayed vertically.

Conclusion

To make text vertical (rotated 90 degrees) in React Native, we can set the transform style.

Categories
JavaScript Answers

How to export an imported module in a single line with JavaScript?

Sometimes, we want to export an imported module in a single line with JavaScript.

In this article, we’ll look at how to export an imported module in a single line with JavaScript.

How to export an imported module in a single line with JavaScript?

To export an imported module in a single line with JavaScript, we can use the export keyword.

For instance, we write:

export {default as Module} from './Module/Module';

to export the ./Module/Module module as a default export.

Then we can import the exported module as a default import in another module.

Conclusion

To export an imported module in a single line with JavaScript, we can use the export keyword.

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.