Categories
JavaScript Answers React Native Answers

How to create a UI with box shadow in React Native?

Sometimes, we want to create a UI with box shadow in React Native.

In this article, we’ll look at how to create a UI with box shadow in React Native.

How to create a UI with box shadow in React Native?

To create a UI with box shadow in React Native, we can set some 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() {
  return (
    <View>
      <View
        style={{
          shadowColor: '#000',
          shadowOffset: { width: 0, height: 1 },
          shadowOpacity: 0.8,
          shadowRadius: 1,
        }}>
        <Text>hello world</Text>
      </View>
    </View>
  );
}

to add a view with a box shadow around it.

shadowOffset adds the shadow effect.

shadowColor sets the shadow color.

And shadowOpacity sets the shadow opacity.

Conclusion

To create a UI with box shadow in React Native, we can set some shadow styles.

Categories
JavaScript Answers React Native Answers

How to fix the React Native FlatList last item not visible issue?

Sometimes, we want to fix the React Native FlatList last item not visible issue.

In this article, we’ll look at how to fix the React Native FlatList last item not visible issue.

How to fix the React Native FlatList last item not visible issue?

To fix the React Native FlatList last item not visible issue, we can add bottom padding to the FlatList.

For instance, we write:

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

const DATA = Array(100)
  .fill()
  .map((_, i) => ({ id: i, title: i }));

const Item = ({ title }) => (
  <View>
    <Text>{title}</Text>
  </View>
);

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;

  return (
    <View>
      <FlatList
        style={{ height: '100%', paddingBottom: 50 }}
        data={DATA}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
}

to add a FlatList and set its paddingBottom style to 50 pixels to add some padding at the bottom of the FlatList.

We render the item with the renderitem function to render each DATA entry.

And we set keyExtractor to a function to return the unique ID property value from the DATA entries.

Conclusion

To fix the React Native FlatList last item not visible issue, we can add bottom padding to the FlatList.

Categories
JavaScript Answers React Native Answers

How to add a rounded image with a border with React Native?

Sometimes, we want to add a rounded image with a border with React Native.

In this article, we’ll look at how to add a rounded image with a border with React Native.

How to add a rounded image with a border with React Native?

To add a rounded image with a border with React Native, we can set the borderRadius and overflow styles.

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() {
  return (
    <View>
      <Image
        source={{ uri: 'https://picsum.photos/200/300' }}
        style={{
          width: 150,
          height: 150,
          borderRadius: '50%',
          overflow: 'hidden',
          borderWidth: 3,
          borderColor: 'red',
        }}
      />
    </View>
  );
}

to set borderRadius to '50%' to make the Image round.

And we set the width and height of the Image to set the dimensions.

We set overflow to 'hidden' so the Image stays in the circle.

Conclusion

To add a rounded image with a border with React Native, we can set the borderRadius and overflow styles.

Categories
JavaScript Answers React Native Answers

How to add a full width button with flexbox in React Native?

Sometimes, we want to add a full width button with flexbox in React Native.

In this article, we’ll look at how to add a full width button with flexbox in React Native.

How to add a full width button with flexbox in React Native?

To add a full width button with flexbox in React Native, we can set the alignSelf style to 'stretch'.

For instance, we write:

import * as React from 'react';
import { View, Button } 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>
      <Button title="button" style={{ alignSelf: 'stretch' }} />
    </View>
  );
}

to add a Button with the alignSelf style set to 'stretch' to make the button fill the width of the screen.

Conclusion

To add a full width button with flexbox in React Native, we can set the alignSelf style to 'stretch'.

Categories
JavaScript Answers React Native Answers

How to use await keyword on React Native?

Sometimes, we want to use await keyword on React Native.

In this article, we’ll look at how to use await keyword on React Native.

How to use await keyword on React Native?

To use await keyword on React Native, we can use it in an async function.

For instance, we write:

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

export default function App() {
  const getAndSetData = async () => {
    const key = 'data';
    await AsyncStorage.setItem(key, 'value');
    const value = await AsyncStorage.getItem(key);
    console.log(value);
  };

  React.useEffect(() => {
    getAndSetData();
  }, []);

  return <View></View>;
}

to use await when calling AsyncStorage.getItem and AsyncStorage.setItem.

Using await will make sure the promises returned by both methods will be done before the next line is run.

We call getAndSetData in the useEffect callback that’s called with an empty array to run it when the component mounts.

Conclusion

To use await keyword on React Native, we can use it in an async function.