Categories
JavaScript Answers React Native Answers

How to align text to the top multiline TextInput with React Native?

Sometimes, we want to align text to the top multiline TextInput with React Native.

In this article, we’ll look at how to align text to the top multiline TextInput with React Native.

How to align text to the top multiline TextInput with React Native?

To align text to the top multiline TextInput with React Native, we can set the textAlignVertical style to 'top'.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <TextInput
        style={{ textAlignVertical: 'top' }}
        numberOfLines={5}
        placeholder="Text"
        multiline
      />
    </View>
  );
}

to add the multiline prop to make the TextInput a multiline input.

We set the numberOfLines to set the text input height to 5 lines high.

And we set textAlignVertical to 'top' to align the entered text to the top.

Conclusion

To align text to the top multiline TextInput with React Native, we can set the textAlignVertical style to 'top'.

Categories
JavaScript Answers React Native Answers

How to call multiple functions when onPress is clicked with React Native?

Sometimes, we want to call multiple functions when onPress is clicked with React Native.

In this article, we’ll look at how to call multiple functions when onPress is clicked with React Native.

How to call multiple functions when onPress is clicked with React Native?

To call multiple functions when onPress is clicked with React Native, we can assign onPress to a function that calls all the functions.

For instance, we write:

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

export default function App() {
  const foo = () => {
    console.log('foo');
  };
  const bar = () => {
    console.log('bar');
  };
  const baz = () => {
    console.log('baz');
  };
  const onPress = () => {
    foo();
    bar();
    baz();
  };

  return (
    <View>
      <Button onPress={onPress} title="button" />
    </View>
  );
}

to define the onPress function that calls foo, bar, and baz.

Then we set the onPress prop to the onPress function.

As a result, when we click the button, we see 'foo', 'bar' and 'baz' logged.

Conclusion

To call multiple functions when onPress is clicked with React Native, we can assign onPress to a function that calls all the functions.

Categories
JavaScript Answers React Native Answers

How to use Lodash debounce in a React Native app?

Sometimes, we want to use Lodash debounce in a React Native app.

In this article, we’ll look at how to use Lodash debounce in a React Native app.

How to use Lodash debounce in a React Native app?

To use Lodash debounce in a React Native app, we can call debounce with the function we want to debounce.

For instance, we write:

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

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

  return (
    <View>
      <TextInput onChangeText={onChangeText} />
    </View>
  );
}

to assign onChangeText to a function that debounce returns which delays setText by 500 milliseconds.

And then we set that as the value of the TextInput onChangeText prop.

Conclusion

To use Lodash debounce in a React Native app, we can call debounce with the function we want to debounce.

Categories
JavaScript Answers React Native Answers

How to fix onclick function is not working in a React Native app?

Sometimes, we want to fix onclick function is not working in a React Native app.

In this article, we’ll look at how to fix onclick function is not working in a React Native app.

How to fix onclick function is not working in a React Native app?

To fix onclick function is not working in a React Native app, we should replace onclick with onPress.

For instance, we write:

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

export default function App() {
  const onPress = () => console.log('pressed');

  return (
    <View>
      <TouchableOpacity onPress={onPress}>
        <View>
          <Text>...</Text>
        </View>
      </TouchableOpacity>
    </View>
  );
}

to set the onPress prop to the onPress function.

Now when we press the text, we should see 'pressed' logged.

Conclusion

To fix onclick function is not working in a React Native app, we should replace onclick with onPress.

Categories
JavaScript Answers React Native Answers

How to add a PDF viewer in React Native?

Sometimes, we want to add a PDF viewer in React Native.

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

How to add a PDF viewer in React Native?

To add a PDF viewer in React Native, we can add a WebView and use Google Drive’s PDF viewer.

For instance, we write:

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

export default function App() {
  const pdfUrl = 'http://www.africau.edu/images/default/sample.pdf';

  return (
    <View style={{ flex: 1 }}>
      <WebView
        source={{
          uri: `https://drive.google.com/viewerng/viewer?embedded=true&url=${pdfUrl}`,
        }}
      />
    </View>
  );
}

to add a WebView with the source set to an object with the uri set to the Google Drive PDF viewer URL.

As a result, we should see the PDF viewer rendered in the WebView.

How to add a PDF viewer in React Native?

To add a PDF viewer in React Native, we can add a WebView and use Google Drive’s PDF viewer.