Categories
JavaScript Answers React Native Answers

How to hide the scrollbar in a FlatList with React Native in Android?

Sometimes, we want to hide the scrollbar in a FlatList with React Native in Android.

In this article, we’ll look at how to hide the scrollbar in a FlatList with React Native in Android.

How to hide the scrollbar in a FlatList with React Native in Android?

To hide the scrollbar in a FlatList with React Native in Android, we can set the showsVerticalScrollIndicator and showsHorizontalScrollIndicator props to false.

For instance, we write:

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

const DATA = Array(100)
  .fill()
  .map((_, i) => ({ id: i, title: i }));

const Item = ({ title }) => (
  <View>
    <Text>{title}</Text>
  </View>
);

const App = () => {
  const renderItem = ({ item }) => <Item title={item.title} />;

  return (
    <View>
      <FlatList
        style={{ height: '200px' }}
        showsVerticalScrollIndicator={false}
        showsHorizontalScrollIndicator={false}
        data={DATA}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
};
export default App;

to add a FlatList with the showsVerticalScrollIndicator and showsHorizontalScrollIndicator props set to false to hide the vertical and horizontal scrollbar respectively.

Conclusion

To hide the scrollbar in a FlatList with React Native in Android, we can set the showsVerticalScrollIndicator and showsHorizontalScrollIndicator props to false.

Categories
JavaScript Answers React Native Answers

How to require image module using dynamic names with React Native?

Sometimes, we want to require image module using dynamic names with React Native.

In this article, we’ll look at how to require image module using dynamic names with React Native.

How to require image module using dynamic names with React Native?

To require image module using dynamic names with React Native, we can call require with different image path strings according to another value.

For instance, we write:

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

const App = () => {
  const [showFirstImg, setShowFirstImg] = React.useState(true);
  return (
    <View>
      <Button title="toggle" onPress={() => setShowFirstImg((s) => !s)} />
      {showFirstImg ? (
        <Image source={require('./test1.png')} />
      ) : (
        <Image source={require('./test2.jpg')} />
      )}
    </View>
  );
};
export default App;

to add 2 Images with different source values.

We call require to include the images in the component.

And we use the showFirstImg to control which image is shown.

We add a Button to toggles the showFirstImg value when pressed.

Conclusion

To require image module using dynamic names with React Native, we can call require with different image path strings according to another value.

Categories
JavaScript Answers React Native Answers

How to fix the issue where user cannot write first letter with noncapital with React Native?

Sometimes, we want to fix the issue where user cannot write first letter with noncapital with React Native

In this article, we’ll look at how to fix the issue where user cannot write first letter with noncapital with React Native.

How to fix the issue where user cannot write first letter with noncapital with React Native?

To fix the issue where user cannot write first letter with noncapital with React Native, we can set the autoCapitalize prop of the TextInput to 'none'.

For instance, we write:

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

const App = () => {
  return (
    <View>
      <TextInput placeholder="name" autoCapitalize="none" />
    </View>
  );
};
export default App;

to set the autoCapitalize prop to 'none' to disable auto-capitalizing the first word of the input value.

Conclusion

To fix the issue where user cannot write first letter with noncapital with React Native, we can set the autoCapitalize prop of the TextInput to 'none'.

Categories
JavaScript Answers React Native Answers

How to change the width of the View by text inside with React Native?

Sometimes, we want to change the width of the View by text inside with React Native.

In this article, we’ll look at how to change the width of the View by text inside with React Native.

How to change the width of the View by text inside with React Native?

To change the width of the View by text inside with React Native, we can set the alignSelf property.

For instance, we write:

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

const App = () => {
  return (
    <View style={{ backgroundColor: 'orange', alignSelf: 'flex-start' }}>
      <Text style={{ color: 'blue' }}>hello world</Text>
    </View>
  );
};
export default App;

to set alignSelf to 'flex-start' to make the View display like a block element in HTML.

Conclusion

To change the width of the View by text inside with React Native, we can set the alignSelf property.

Categories
JavaScript Answers React Native Answers

How to get the current scroll position of ScrollView in React Native?

Sometimes, we want to get the current scroll position of ScrollView in React Native.

In this article, we’ll look at how to get the current scroll position of ScrollView in React Native.

How to get the current scroll position of ScrollView in React Native?

To get the current scroll position of ScrollView in React Native, we can set the onScroll prop of the ScrollView to a function that takes the scroll event object as an argument.

For instance, we write:

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

const a = Array(50)
  .fill()
  .map((_, i) => i);

const App = () => {
  const scrollViewRef = React.useRef();
  const [pos, setPos] = React.useState(0);
  const [arr, setArr] = React.useState(a);

  return (
    <View style={{ flex: 1 }}>
      <Text>pos: {pos}</Text>
      <ScrollView
        style={{ height: '200px' }}
        onScroll={(e) => setPos(e.nativeEvent.contentOffset.y)}>
        {arr.map((i) => (
          <Text>{i}</Text>
        ))}
      </ScrollView>
    </View>
  );
};
export default App;

to get the scroll position of the scroll view with e.nativeEvent.contentOffset.y.

We call setPos to set that as the value of pos.

Now when we scroll, we see the pos value change.

Conclusion

To get the current scroll position of ScrollView in React Native, we can set the onScroll prop of the ScrollView to a function that takes the scroll event object as an argument.