Categories
JavaScript Answers React Native Answers

How disable options on React Native TextInput?

Sometimes, we want to disable options on React Native TextInput.

In this article, we’ll look at how to disable options on React Native TextInput.

How disable options on React Native TextInput?

To disable options on React Native TextInput, we can set the editable and selectTextOnFocus props to false.

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';

export default function App() {
  return (
    <View>
      <TextInput
        editable={false}
        selectTextOnFocus={false}
        style={{ padding: 30 }}
        placeholder="Name"
      />
    </View>
  );
}

to set them both to false.

Then we won’t see any popup options displayed when we focus on the text input.

editable set to false disables typing in the input.

selectTextOnFocus set to false means text won’t be selected when we focus on it.

Conclusion

To disable options on React Native TextInput, we can set the editable and selectTextOnFocus props to false.

Categories
JavaScript Answers React Native Answers

How to retrieve actual image sizes with React Native?

Sometimes, we want to retrieve actual image sizes with React Native.

In this article, we’ll look at how to retrieve actual image sizes with React Native.

How to retrieve actual image sizes with React Native?

To retrieve actual image sizes with React Native, we can use the Image.getSize method.

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';

export default function App() {
  const uri = 'https://picsum.photos/200/300';
  const size = Image.getSize(uri, (width, height) =>
    console.log(width, height)    
  );
  console.log(size);

  return (
    <View>
      <Image source={{ uri }} style={{ width: 200, height: 300 }} />
    </View>
  );
}

to call Image.getSize with the image uri and callback that takes the image width and height as parameters.

As a result, we see the image at the given uri‘s width and height logged.

Conclusion

To retrieve actual image sizes with React Native, we can use the Image.getSize method.

Categories
JavaScript Answers React Native Answers

How to add text shadow in React Native?

Sometimes, we want to add text shadow in React Native.

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

How to add text shadow in React Native?

To add text shadow in React Native, we can set some text 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() {
  const [height, setHeight] = React.useState(30);

  return (
    <View>
      <Text
        style={{
          textShadowColor: 'rgba(0, 0, 0, 0.75)',
          textShadowOffset: { width: -1, height: 1 },
          textShadowRadius: 10,
        }}>
        hello world
      </Text>
    </View>
  );
}

to set the textShadowColor to the text shadow color.

We set textShadowOffset to add depth to the shadow.

And we set textShadowRadius to set the shadow radius.

Conclusion

To add text shadow in React Native, we can set some text shadow styles.

Categories
JavaScript Answers React Native Answers

How to grow height of TextInput upon text wrapping with React Native?

Sometimes, we want to grow height of TextInput upon text wrapping with React Native.

In this article, we’ll look at how to grow height of TextInput upon text wrapping with React Native.

How to grow height of TextInput upon text wrapping with React Native?

To grow height of TextInput upon text wrapping with React Native, we can set the height of the TextInput to the content’s height when the content size changes.

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';

export default function App() {
  const [height, setHeight] = React.useState(30);

  return (
    <View>
      <TextInput
        multiline
        style={{ height }}
        onContentSizeChange={(event) => {
          setHeight(event.nativeEvent.contentSize.height);
        }}
      />
    </View>
  );
}

to set the onContentSizeChange prop to a function that calls setHeight with event.nativeEvent.contentSize.height to set height to event.nativeEvent.contentSize.height.

Then we set the height style of the TextInput to height.

Now as we type, the TextInput will change size to fit the input value.

Conclusion

To grow height of TextInput upon text wrapping with React Native, we can set the height of the TextInput to the content’s height when the content size changes.

Categories
JavaScript Answers React Native Answers

How to set the minimum width or height in React Native

Sometimes, we want to set the minimum width or height in React Native.

In this article, we’ll look at how to set the minimum width or height in React Native.

How to set the minimum width or height in React Native

To set the minimum width or height in React Native, we can set the minWidth and minHeight properties.

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 dismissKeyboard from 'react-native-dismiss-keyboard';

export default function App() {
  return (
    <View
      style={{
        minWidth: '20%',
        maxWidth: 500,
        minHeight: '10%',
        maxHeight: 150,
      }}>
      <Text>hello world</Text>
    </View>
  );
}

to set the min width with minWidth and min height with minHeight.

We can also set the max width with maxWidth and max height with maxHeight.

Conclusion

To set the minimum width or height in React Native, we can set the minWidth and minHeight properties.