Categories
JavaScript Answers React Native Answers

How to set margin or padding with shorthands in React Native?

Sometimes, we want to set margin or padding with shorthands in React Native.

In this article, we’ll look at how to set margin or padding with shorthands in React Native.

How to set margin or padding with shorthands in React Native?

To set margin or padding with shorthands in React Native, we can use the marginVertical, marginHorizontal, paddinHorizontal and paddingVertical properties.

For instance, we write:

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

export default function App() {
  return (
    <View
      style={{
        marginVertical: 10,
        marginHorizontal: 20,
        paddingHorizontal: 20,
        paddingVertical: 20,
      }}>
      <Text>hello</Text>
    </View>
  );
}

to set the margin and padding with the properties.

marginVertical set the vertical margins.

marginHorizontal set the horizontal margins.

paddingHorizontal set the horizontal paddings.

paddingVertical set the vertical paddings.

Conclusion

To set margin or padding with shorthands in React Native, we can use the marginVertical, marginHorizontal, paddinHorizontal and paddingVertical properties.

Categories
JavaScript Answers React Native Answers

How to update array state in React Native?

Sometimes, we want to update array state in React Native.

In this article, we’ll look at how to update array state in React Native.

How to update array state in React Native?

To update array state in React Native, we can call the state setter function with a function that returns the new array we want.

For instance, we write:

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

export default function App() {
  const [arr, setArr] = React.useState(['foo', 'bar', 'baz']);

  return (
    <View>
      {arr.map((a, i) => {
        return (
          <TextInput
            value={a}
            key={i}
            onChangeText={(t) => {
              setArr((arr) => {
                const newArr = [...arr];
                newArr[i] = t;
                return newArr;
              });
            }}
          />
        );
      })}
      <Text>{arr.join(', ')}</Text>
    </View>
  );
}

to define the arr state with useState.

Then we call arr.map with a callback that renders a TextInput.

Next, we set onChangeText to a function that calls setArr with a callback that updates the arr array.

In the function, we make a copy of arr and assign it to newArr.

Then we assign newArr[i] to the input value t.

And then we return newArr.

As a result, when we type something into the text input, we see the Text display change to the typed value.

Conclusion

To update array state in React Native, we can call the state setter function with a function that returns the new array we want.

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.