Categories
JavaScript Answers React Native Answers

How to run code when an animation is complete with React Native?

Sometimes, we want to run code when an animation is complete with React Native.

In this article, we’ll look at how to run code when an animation is complete with React Native.

How to run code when an animation is complete with React Native?

To run code when an animation is complete with React Native, we can put the code that we want to run when the animation is done in the callback we pass into start.

For instance, we write:

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

const App = () => {
  const fadeAnim = React.useRef(new Animated.Value(100)).current;
  const [finished, setFinished] = React.useState(false);

  const fadeOut = () => {
    Animated.timing(fadeAnim, {
      toValue: 0,
      duration: 3000,
    }).start(() => setFinished(true));
  };

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

  return (
    <View>
      <Animated.View
        style={[
          {
            opacity: fadeAnim,
          },
        ]}>
        <Text>Fading View</Text>
      </Animated.View>
      {finished && <Text>Finished</Text>}
    </View>
  );
};
export default App;

to add an animated view.

We set the opacity style to fadeAnim to animate the opacity to fade out the ‘Fading View’ text.

To start the animation, we call Animated.timing with the fadeAnim animation variable.

The duration of the animation is 3000 milliseconds.

And we animate the opacity to 0 as specified by the toValue.

We call start with a function that calls setFinished to set finished to true.

And then we call fadeOut in the useEffect hook callback which is called with an empty array to run the animation when the page loads.

As a result, we see the Fading Out text fade and the Finished text shown after the animation is done.

Conclusion

To run code when an animation is complete with React Native, we can put the code that we want to run when the animation is done in the callback we pass into start.

Categories
JavaScript Answers React Native Answers

How to align text input content to the top in React Native?

Sometimes, we want to align text input content to the top in React Native.

In this article, we’ll look at how to align text input content to the top in React Native.

How to align text input content to the top in React Native?

To align text input content to the top in React Native, we can set the textAlignVertical styles property to true.

For instance, we write:

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

const App = () => {
  return (
    <View>
      <TextInput
        style={{
          flex: 1,
          width: '100%',
          height: 150,
          border: '1px solid black',
          textAlignVertical: 'top',
        }}
        multiline
        numberOfLines={10}
      />
    </View>
  );
};
export default App;

to set textAlignVertical to 'top' in the styles object to make the inputted text align to the top.

Conclusion

To align text input content to the top in React Native, we can set the textAlignVertical styles property to true.

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.