Categories
JavaScript Answers React Native Answers

How to keep a ScrollView scrolled to the bottom with React Native?

Sometimes, we want to keep a ScrollView scrolled to the bottom with React Native.

In this article, we’ll look at how to keep a ScrollView scrolled to the bottom with React Native.

How to keep a ScrollView scrolled to the bottom with React Native?

To keep a ScrollView scrolled to the bottom with React Native, we can assign a ref to the ScrollView and call scrollToEnd on it.

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 [arr, setArr] = React.useState(a);

  return (
    <View style={{ flex: 1 }}>
      <Button onPress={() => setArr([...arr, ...a])} title="add" />
      <ScrollView
        style={{ height: '200px' }}
        ref={scrollViewRef}
        onContentSizeChange={() =>
          scrollViewRef.current.scrollToEnd({ animated: true })
        }>
        {arr.map((i) => (
          <Text>{i}</Text>
        ))}
      </ScrollView>
    </View>
  );
};
export default App;

to assign a ref to the ScrollView.

Then we set onContentSizeChange to a function that calls scrollViewRef.current.scrollToEnd with { animated: true } to scroll to the bottom when the scroll height of the ScrollView changes.

We add the ScrollView content in between the ScrollView tags.

And we add a Button to add some content to the arr array which is rendered in the ScrollView.

Conclusion

To keep a ScrollView scrolled to the bottom with React Native, we can assign a ref to the ScrollView and call scrollToEnd on it.

Categories
JavaScript Answers React Native Answers

How to fetch data from local JSON file with React Native?

Sometimes, we want to fetch data from local JSON file with React Native.

In this article, we’ll look at how to fetch data from local JSON file with React Native.

How to fetch data from local JSON file with React Native?

To fetch data from local JSON file with React Native, we can import the JSON file with import.

For instance, we write:

customData.json

{
  "foo": 1
}

App.js

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


const App = () => {
  return (
    <View>
      <Text>{JSON.stringify(customData)}</Text>
    </View>
  );
};
export default App;

to import the content of customData.json as a default import.

Then we can use customData in our components as an object.

Therefore, we call JSON.stringify with customData to convert it to a JSON string.

And {"foo":1} is displayed on the screen.

Conclusion

To fetch data from local JSON file with React Native, we can import the JSON file with import.

Categories
JavaScript Answers React Native Answers

How to set the width of a View to 80% of parent in React Native?

Sometimes, we want to set the width of a View to 80% of parent in React Native.

In this article, we’ll look at how to set the width of a View to 80% of parent in React Native.

How to set the width of a View to 80% of parent in React Native?

To set the width of a View to 80% of parent in React Native, we can set the flexbox and width styles.

For instance, we write:

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

const widthProportion = '80%';
const heightProportion = '40%';

const styles = StyleSheet.create({
  screen: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#5A9BD4',
  },
  box: {
    width: widthProportion,
    height: heightProportion,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'orange',
  },
  text: {
    fontSize: 18,
  },
});

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

to add the box style that sets the width and height of the box.

We set the width to '80%' to make the box fill 80% of the width of the parent.

Then we set justifyContent and alignItems to 'center' to center align the View box horizontally and vertically respectively.

Conclusion

To set the width of a View to 80% of parent in React Native, we can set the flexbox and width styles.

Categories
JavaScript Answers React Native Answers

How to set absolute positioning horizontal center with React Native?

Sometimes, we want to set absolute positioning horizontal center with React Native.

In this article, we’ll look at how to set absolute positioning horizontal center with React Native.

How to set absolute positioning horizontal center with React Native?

To set absolute positioning horizontal center with React Native, we can set the position and flexbox styles.

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
      style={{
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        justifyContent: 'center',
        alignItems: 'center',
      }}>
      <Text>Centered text</Text>
    </View>
  );
};
export default App;

to set the position of the view to 'absolute' to add absolute positioning.

Then we set top, left, right, and bottom to 0 to set the position,

And then we set justifyContent and alignItems to 'center' to center align horizontally and vertically respectively.

Conclusion

To set absolute positioning horizontal center with React Native, we can set the position and flexbox styles.

Categories
JavaScript Answers React Native Answers

How to set responsive font size with React Native?

Sometimes, we want to set responsive font size with React Native.

In this article, we’ll look at how to set responsive font size with React Native.

How to set responsive font size with React Native?

To set responsive font size with React Native, we can use the Dimensions object to get the values to calculate the font size.

For instance, we write:

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

const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');

const scale = SCREEN_WIDTH / 320;

const normalize = (size) => {
  const newSize = size * scale;
  if (Platform.OS === 'ios') {
    return Math.round(PixelRatio.roundToNearestPixel(newSize));
  } else {
    return Math.round(PixelRatio.roundToNearestPixel(newSize)) - 2;
  }
};

const App = () => {
  return (
    <View>
      <Text style={{ fontSize: normalize(30) }}>hello</Text>
    </View>
  );
};
export default App;

to define the normalize function that calculates newSize by multipklying size and scale`.

We get the scale by dividing SCREEN_WIDTH by a number.

Then we return the actual font size depending on the platform.

We use PixelRatio.roundToNearestPixel to round to the nearest pixel.

Finally. we set the fontSize of the Text.

Conclusion

To set responsive font size with React Native, we can use the Dimensions object to get the values to calculate the font size.