Categories
JavaScript Answers React Native Answers

How to create global styles with React Native?

Sometimes, we want to create global styles with React Native.

In this article, we’ll look at how to create global styles with React Native.

How to create global styles with React Native?

To create global styles with React Native, we can create a module that exports the stylesheet object.

For instance, we write:

styles.js

import { StyleSheet } from 'react-native';

export default StyleSheet.create({
  alwaysRed: {
    backgroundColor: 'red',
    height: 100,
    width: 100,
  },
});

App.js

import * as React from 'react';
import { View, Text, Button } from 'react-native';
import { Card } from 'react-native-paper';
import styles from './styles';

const App = () => {
  return (
    <View>
      <Text style={styles.alwaysRed}>hello</Text>
    </View>
  );
};
export default App;

We call StyleSheet.create in styles.js to create a stylesheet object.

Then we import it with import styles from './styles'; in App.js.

Next, we apply the styles to the Text component by setting style to styles.alwaysRed.

Conclusion

To create global styles with React Native, we can create a module that exports the stylesheet object.

Categories
JavaScript Answers React Native Answers

How to add if-else statement inside JSX with React Native?

Sometimes, want to add if-else statement inside JSX with React Native.

In this article, we’ll look at how to add if-else statement inside JSX with React Native.

How to add if-else statement inside JSX with React Native?

To add if-else statement inside JSX with React Native, we can put them in a function.

For instance, we write:

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

const App = () => {
  const [head, setHead] = React.useState(true);

  const showValue = () => {
    if (head) {
      return <Text>head</Text>;
    } else {
      return <Text>tail</Text>;
    }
  };

  return (
    <View>
      <Button title="flip" onPress={() => setHead((h) => !h)} />
      {showValue()}
    </View>
  );
};
export default App;

to create the showValue function that checks the value of the head state to render the text we want.

Then we add a Button to toggle the head value on press.

Finally, we call showValue to render the Text component returned in the function.

Conclusion

To add if-else statement inside JSX with React Native, we can put them in a function.

Categories
JavaScript Answers React Native Answers

How to make text bold, italic, or underline in React Native?

Sometimes, we want to make text bold, italic, or underline in React Native.

In this article, we’ll look at how to make text bold, italic, or underline in React Native.

How to make text bold, italic, or underline in React Native?

To make text bold, italic, or underline in React Native, we can set various styles.

For instance, we write:

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

const styles = StyleSheet.create({
  bold: { fontWeight: 'bold' },
  italic: { fontStyle: 'italic' },
  underline: { textDecorationLine: 'underline' },
});

const App = () => {
  return (
    <View>
      <Text style={styles.bold}>bold</Text>
      <Text style={styles.italic}>italic</Text>
      <Text style={styles.underline}>underlined</Text>
    </View>
  );
};
export default App;

to create the bold style by setting fontWeight to 'bold'.

We create the italic style by setting fontStyle to 'italic'.

And we create the underline style by setting textDecorationLine to 'underline'.

Now we should see bold, italic, and underlined text ordered from top to bottom.

Conclusion

To make text bold, italic, or underline in React Native, we can set various styles.

Categories
JavaScript Answers React Native Answers

How to add border radius only for 1 corner with React Native?

Sometimes, we want to add border radius only for 1 corner with React Native.

In this article, we’ll look at how to add border radius only for 1 corner with React Native.

How to add border radius only for 1 corner with React Native?

To add border radius only for 1 corner with React Native, we can set the borderBottomLeftRadius, borderBottomRightRadius, borderTopLeftRadius, and borderTopRightRadius individually.

For instance, we write:

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

const App = () => {
  return (
    <View>
      <View
        style={{
          borderBottomLeftRadius: 5,
          borderBottomRightRadius: 0,
          borderTopLeftRadius: 0,
          borderTopRightRadius: 0,
          border: '1px solid black',
        }}>
        <Text>hello world</Text>
      </View>
    </View>
  );
};
export default App;

to set borderBottomLeftRadius to 5 and the rest to 0 to add a border radius to the bottom left corner only.

Conclusion

To add border radius only for 1 corner with React Native, we can set the borderBottomLeftRadius, borderBottomRightRadius, borderTopLeftRadius, and borderTopRightRadius individually.

Categories
JavaScript Answers React Native Answers

How to clear the React Native cache?

Sometimes, we want to clear the React Native cache.

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

How to clear the React Native cache?

To clear the React Native cache, we can run various commands.

We can clear cache from the react-native command with.

react-native start --reset-cache

With npm, we run:

npm start -- --reset-cache

And we can run Expo to clear the cache by running

expo start -c

Conclusion

To clear the React Native cache, we can run various commands.