Categories
JavaScript Answers React Native Answers

How to fit Image in containing View with React Native?

Sometimes, we want to fit Image in containing View with React Native.

In this article, we’ll look at how to fit Image in containing View with React Native.

How to fit Image in containing View with React Native?

To fit Image in containing View with React Native, we can set the width of the Image to '100%'.

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';
import { WebView } from 'react-native-webview';

export default function App() {
  const [text, setText] = React.useState();
  return (
    <View style={{ width: 200, height: 300 }}>
      <Image
        source="https://picsum.photos/200/300"
        style={{
          width: '100%',
          height: undefined,
          aspectRatio: 1,
        }}
      />
    </View>
  );
}

to set the style prop of the Image to

{
  width: '100%',
  height: undefined,
  aspectRatio: 1,
}

to make the width of the image fit to the View‘s width.

And we set the aspectRatio to 1 to make the height the same as the width.

Conclusion

To fit Image in containing View with React Native, we can set the width of the Image to '100%'.

Categories
JavaScript Answers React Native Answers

How to add a text area in React Native?

Sometimes, we want to add a text area in React Native.

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

How to add a text area in React Native?

To add a text area in React Native, we can use a TextInput with the multiline and the numberOfLines props.

For instance, we write:

import * as React from 'react';
import { TextInput, View, Text } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
import { WebView } from 'react-native-webview';

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

  return (
    <View style={{ flex: 1 }}>
      <TextInput
        multiline
        numberOfLines={4}
        onChangeText={(text) => setText(text)}
        value={text}
      />
      <Text>{text}</Text>
    </View>
  );
}

to add a TextInput with the multiline prop to add a text area.

Then we set numberOfLines to 4 to make the text input line 4 lines tall.

We set the onChangeText prop to a function that sets the text state to the input value.

And we set the value prop to text to show the input value.

Conclusion

To add a text area in React Native, we can use a TextInput with the multiline and the numberOfLines props.

Categories
JavaScript Answers React Native Answers

How to render HTML in React Native?

Sometimes, we want to render HTML in React Native.

In this article, we’ll look at how to render HTML in React Native.

How to render HTML in React Native?

To render HTML in React Native, we can use a WebView.

For instance, we write:

import * as React from 'react';
import { Text, View } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
import { WebView } from 'react-native-webview';

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <WebView
        originWhitelist={['*']}
        style={{ marginTop: 50, marginBottom: 50 }}
        source={{ html: '<h1><b>hello world</b></h1>' }}
      />
    </View>
  );
}

to set the source prop to { html: '<h1><b>hello world</b></h1>' } to display ‘hello world’ in bold on the screen.

Conclusion

To render HTML in React Native, we can use a WebView.

Categories
JavaScript Answers React Native Answers

How to change the styling of TextInput placeholder in React Native?

Sometimes, we want to change the styling of TextInput placeholder in React Native.

In this article, we’ll look at how to change the styling of TextInput placeholder in React Native.

How to change the styling of TextInput placeholder in React Native?

To change the styling of TextInput placeholder in React Native, we can set the placeholderTextColor prop.

For instance, we write:

import * as React from 'react';
import { Text, View, TextInput } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <TextInput placeholderTextColor="red" placeholder="Name" />
    </View>
  );
}

to set the placeholderTextColor to 'red' to set the placeholder color of the input to red.

Conclusion

To change the styling of TextInput placeholder in React Native, we can set the placeholderTextColor prop.

Categories
JavaScript Answers React Native Answers

How to add base64 encoded images with React Native?

Sometimes, we want to add base64 encoded images with React Native.

In this article, we’ll look at how to add base64 encoded images with React Native.

How to add base64 encoded images with React Native?

To add base64 encoded images with React Native, we can include the base64 image string as the value of the source prop value.

For instance, we write:

import * as React from 'react';
import { Text, View, Image } from 'react-native';
import Constants from 'expo-constants';
import AssetExample from './components/AssetExample';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

const base64Icon =
  'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZV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z0ogwFKm7GgCjgyZMihCLCkc0nkIAnIMVRw2UhDBGp5fcurGOyLfbhVtJwLdJkY8oscZCsFPBk5spiNaoTC4hnqk801Qi2zLQyD2NlcWWP5GepN5TOtSxg1QwrV01itpECG2kaLy3AYiCWxcRozQWyp9pNMDWePDI4QgVpbx5eo7a+mHFOqAxUQVeRhdrLjdFFQggqo5tqVeSS456UEQgWE4/RBboxyC4AKCEI9Wu9lUl8PEGAAV7NY4hyx8voAAAAASUVORK5CYII=';

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <Image source={{ uri: base64Icon }} style={{ width: 20, height: 20 }} />
    </View>
  );
}

to add an Image into the View.

And we set the source prop to { uri: base64Icon } to use the base64Icon string as the image URL.

Conclusion

To add base64 encoded images with React Native, we can include the base64 image string as the value of the source prop value.