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.

Categories
JavaScript Answers React Native Answers

How to add a circle image button with React Native?

Sometimes, we want to add a circle image button with React Native.

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

How to add a circle image button with React Native?

To add a circle image button with React Native, we can use the TouchableOpacity and Icon components.

For instamce, we write:

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

export default function App() {
  return (
    <View>
      <TouchableOpacity
        style={{
          borderWidth: 1,
          borderColor: 'rgba(0,0,0,0.2)',
          alignItems: 'center',
          justifyContent: 'center',
          width: 100,
          height: 100,
          backgroundColor: '#fff',
          borderRadius: 50,
        }}>
        <Icon name="chevron-right" size={30} color="#01a699" />
      </TouchableOpacity>
    </View>
  );
}

to make the TouchableOpacity round with by setting the width and height and set the borderRadius to be half of the width and height.

And then we align the Icon in the center of TouchableOpacity by setting justifyContent and alignItems to 'center'.

And we add a border around the TouchableOpacity component by setting borderWidth and borderColor.

Conclusion

To add a circle image button with React Native, we can use the TouchableOpacity and Icon components.

Categories
JavaScript Answers React Native Answers

How to fix shadow not appearing with React Native?

Sometimes, we want to fix shadow not appearing with React Native.

In this article, we’ll look at how to fix shadow not appearing with React Native.

How to fix shadow not appearing with React Native?

To fix shadow not appearing with React Native, we should set the shadow styles and the elevation style.

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>
      <Text
        style={{
          width: 50,
          height: 30,
          shadowOffset: { width: 5, height: 5 },
          shadowColor: 'black',
          shadowOpacity: 1,
          elevation: 2,
          backgroundColor: '#0000',
        }}>
        hello
      </Text>
    </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.

elevation sets the shadow’s height.

Conclusion

To fix shadow not appearing with React Native, we should set the shadow styles and the elevation style.

Categories
JavaScript Answers React Native Answers

How to change button style on press in React Native?

Sometimes, we want to change button style on press in React Native.

In this article, we’ll look at how to change button style on press in React Native.

How to change button style on press in React Native?

To change button style on press in React Native, we can wrap our button with the TouchableHighlight component.

For instance, we write:

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

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  btnNormal: {
    borderColor: 'blue',
    borderWidth: 1,
    borderRadius: 10,
    height: 30,
    width: 100,
  },
  btnPress: {
    borderColor: 'blue',
    borderWidth: 1,
    height: 30,
    width: 100,
  },
  btn: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    display: 'flex'
  }
});
export default function App() {
  const [isPress, setIsPress] = React.useState(false);

  const touchProps = {
    activeOpacity: 1,
    underlayColor: 'blue',
    style: isPress ? styles.btnPress : styles.btnNormal,
    onHideUnderlay: () => setIsPress(false),
    onShowUnderlay: () => setIsPress(true),
    onPress: () => console.log('hello'),
  };

  return (
    <View style={styles.container}>
      <TouchableHighlight {...touchProps}>
        <Text style={styles.btn}>Click here</Text>
      </TouchableHighlight>
    </View>
  );
}

to define the touchProps object that we use to set the props of TouchableHighlight.

We set onHideUnderlay to a function that runs when the underlay is hidden.

We set onShowUnderlay to a function that runs when the underlay is shown.

Both functions change the isPress state.

And we apply different styles for the TouchableHighlight according to the value of isPress.

Conclusion

To change button style on press in React Native, we can wrap our button with the TouchableHighlight component.