Categories
JavaScript Answers React Native Answers

How to style the standard React Native Android picker?

Sometimes, we want to style the standard React Native Android picker.

In this article, we’ll look at how to style the standard React Native Android picker.

How to style the standard React Native Android picker?

To style the standard React Native Android picker, we can set the color prop on Picker.Item.

For instance, we write:

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

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

  return (
    <View style={{ padding: 30 }}>
      <Picker selectedValue={val} onValueChange={setVal} mode="dropdown">
        <Picker.Item label="hello" value="0" color="blue" />
        <Picker.Item label="world" value="1" color="blue" />
      </Picker>
    </View>
  );
}

to add a Picker.

Then we set the color prop of the Picker.Item to blue to make the drop down text blue.

Conclusion

To style the standard React Native Android picker, we can set the color prop on Picker.Item.

Categories
JavaScript Answers React Native Answers

How to use Image with a local file with React Native?

Sometimes, we want to use Image with a local file with React Native.

In this article, we’ll look at how to use Image with a local file with React Native.

How to use Image with a local file with React Native?

To use Image with a local file with React Native, we can call require to retrieve the image and set that as the source prop’s value.

For instance, we write:

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

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

  return (
    <View>
      <Image source={require('./assets/snack-icon.png')} />
    </View>
  );
}

to call require with a local image path.

And then we set that as the source value to display the image.

Conclusion

To use Image with a local file with React Native, we can call require to retrieve the image and set that as the source prop’s value.

Categories
JavaScript Answers React Native Answers

How to set max length of the TextInput with React Native?

Sometimes, we want to set max length of the TextInput with React Native.

In this article, we’ll look at how to set max length of the TextInput with React Native.

How to set max length of the TextInput with React Native?

To set max length of the TextInput with React Native, we can set the maxLength prop.

For instance, we write:

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

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

  return (
    <View>
      <TextInput value={text} onChangeText={setText} maxLength={10} />
    </View>
  );
}

to set the maxLength prop to 10 to allow users to enter a max of 10 characters into the input.

Conclusion

To set max length of the TextInput with React Native, we can set the maxLength prop.

Categories
JavaScript Answers React Native Answers

How to bind onPress with an argument in React Native?

Sometimes, we want to bind onPress with an argument in React Native.

In this article, we’ll look at how to bind onPress with an argument in React Native.

How to bind onPress with an argument in React Native?

To bind onPress with an argument in React Native, we can create a function that returns the onPress handler function.

For instance, we write:

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

export default function App() {
  const onPress = (arg) => () => {
    console.log(arg);
  };

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

to add a Button with the onPress prop set to the function that we get after we call onPress with 'hello'.

We define onPress to return a function that logs the arg that we passed into the outer function.

Therefore, when we press the button, we see 'hello' logged.

Conclusion

To bind onPress with an argument in React Native, we can create a function that returns the onPress handler function.

Categories
JavaScript Answers React Native Answers

How to add styling with conditional with React Native?

Sometimes, we want to add styling with conditional with React Native.

In this article, we’ll look at how to add styling with conditional with React Native.

How to add styling with conditional with React Native?

To add styling with conditional with React Native, we can use a function.

For instance, we write:

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

export default function App() {
  const [val, setVal] = React.useState();
  const getStyles = () => {
    return {
      border: !val ? '1px solid red' : 'none',
    };
  };

  return (
    <View>
      <TextInput
        value={val}
        onChangeText={setVal}
        style={getStyles()}
        placeholder="name"
      />
    </View>
  );
}

to define the getStyles function that returns an object with the border style according to the value of val.

We use the object getStyles return as the value of style.

And we call setVal when we type into the text input.

Therefore, when the input is empty, we see a red border and no border otherwise.

Conclusion

To add styling with conditional with React Native, we can use a function.