Categories
JavaScript Answers React Native Answers

How to fix AsyncStorage fetches data after rendering issue with React Native?

Sometimes, we want to fix AsyncStorage fetches data after rendering issue with React Native.

In this article, we’ll look at how to fix AsyncStorage fetches data after rendering issue with React Native.

How to fix AsyncStorage fetches data after rendering issue with React Native?

To fix AsyncStorage fetches data after rendering issue with React Native, we can check if the value is fetched before rendering them item we want.

For instance, we write:

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

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

  const getData = async () => {
    await AsyncStorage.setItem('key', 'val');
    const v = await AsyncStorage.getItem('key');
    console.log(v);
    setVal(v);
  };

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

  return <View>{val && <Text>{val}</Text>}</View>;
}

to call AsyncStorage.getItem with the key to get the item with the given key.

Then we call setVal to set val to the returned value.

Next, we call useEffect with a callback that calls getData and an empty array to call getData when App mounts.

And then we check if val is set before rendering Text with

val && <Text>{val}</Text>

Conclusion

To fix AsyncStorage fetches data after rendering issue with React Native, we can check if the value is fetched before rendering them item we want.

Categories
JavaScript Answers React Native Answers

How to add border bottom in React Native?

Sometimes, we want to add border bottom in React Native.

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

How to add border bottom in React Native?

To add border bottom in React Native, we can add border bottom on the View.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ borderBottom: '1px solid blue' }}>
      <Text>foo</Text>
    </View>
  );
}

to set borderBottom to '1px solid blue' on the View.

Now we see a bottom border on below the text and it’s blue.

Conclusion

To add border bottom in React Native, we can add border bottom on the View.

Categories
JavaScript Answers React Native Answers

How to make a component float right with flexbox and React Native?

Sometimes, we want to make a component float right with flexbox and React Native.

In this article, we’ll look at how to make a component float right with flexbox and React Native.

How to make a component float right with flexbox and React Native?

To make a component float right with flexbox and React Native, we can set justifyContent to 'space-between' and set textAlign to 'right'.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
      <Text>foo</Text>
      <Text style={{ textAlign: 'right' }}>bar</Text>
    </View>
  );
}

to set flexDirection to 'row' to set the flex direction to horizontal.

justifyContent is set to 'space-between' so we spread the child components in a row.

Next, we set textAlign to 'right' on the 2nd Text component to align the text to the right.

Conclusion

To make a component float right with flexbox and React Native, we can set justifyContent to 'space-between' and set textAlign to 'right'.

Categories
JavaScript Answers React Native Answers

How to use FormData in React Native?

Sometimes, we want to use FormData in React Native.

In this article, we’ll look at how to use FormData in React Native.

How to use FormData in React Native?

To use FormData in React Native, we can use the FormData constructor.

For instance, we write:

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

export default function App() {
  const submit = async () => {
    const formData = new FormData();
    formData.append('title', 'title');
    formData.append('content', 'content');
    fetch('https://jsonplaceholder.typicode.com', {
      body: formData,
      method: 'POST'
    });
  };

  return (
    <View>
      <Button title="submit" onPress={submit} />
    </View>
  );
}

to define the submit function that creates a FormData instance.

Then we call append with the key and value to add form data entries.

Next, we call fetch with the URL to make the request to and an object with the body set to the formData request body and the request method.

We set the onPress prop of the Button to call submit when we press the button

Conclusion

To use FormData in React Native, we can use the FormData constructor.

Categories
JavaScript Answers React Native Answers

How to change the Android status bar color with React Native?

Sometimes, we want to change the Android status bar color with React Native.

In this article, we’ll look at how to change the Android status bar color with React Native.

How to change the Android status bar color with React Native?

To change the Android status bar color with React Native, we can set the backgroundColor prop of the StatusBar.

For instance, we write:

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

export default function App() {
  return (
    <View>
      <StatusBar backgroundColor="green" barStyle="light-content" />
    </View>
  );
}

to set the backgroundColor prop of StatusBar to 'green'.

Now the status bar would be green.

Conclusion

To change the Android status bar color with React Native, we can set the backgroundColor prop of the StatusBar.