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.

Categories
JavaScript Answers React Native Answers

How to clear React Native TextInput?

Sometimes, we want to clear React Native TextInput.

In this article, we’ll look at how to clear React Native TextInput.

How to clear React Native TextInput?

To clear React Native TextInput, we can set the value with a state and clear the state.

For instance, we write:

import * as React from 'react';
import { View, TextInput, Button } 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>
      <TextInput value={val} onChangeText={setVal} />
      <Button title="clear" onPress={() => setVal('')} />
    </View>
  );
}

to set the value prop to val.

And we set onChangeText to setVal to set val to the inputted value.

Next, we add a Button with the onPress prop set to a function that set val to an empty string with setVal.

Conclusion

To clear React Native TextInput, we can set the value with a state and clear the state.

Categories
JavaScript Answers React Native Answers

How to combine external and inline styles with React Native?

Sometimes, we want to combine external and inline styles with React Native.

In this article, we’ll look at how to combine external and inline styles with React Native.

How to combine external and inline styles with React Native?

To combine external and inline styles with React Native, we can put all the style objects in an array.

For instance, we write:

import * as React from 'react';
import { View, TouchableHighlight, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';
import { Icon } from 'react-native-elements';

const styles = StyleSheet.create({
  button: {
    width: 100,
  },
});

export default function App() {
  return (
    <View>
      <TouchableHighlight style={[styles.button, { backgroundColor: 'yellow' }]}>
        <Icon name="chevron-right" size={30} color="#01a699" />
      </TouchableHighlight>
    </View>
  );
}

to combine the styles.button styles with the backgroundColor style.

Therefore, TouchableHighlight has width 100px and background color yellow.

Conclusion

To combine external and inline styles with React Native, we can put all the style objects in an array.