Categories
JavaScript Answers React Native Answers

How to set width style to a percentage number with React Native?

Sometimes, we want to set width style to a percentage number with React Native.

In this article, we’ll look at how to set width style to a percentage number with React Native.

How to set width style to a percentage number with React Native?

To set width style to a percentage number with React Native, we can use flexbox.

For instance, we write:

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

export default function App() {
  return (
    <View style={{ flexDirection: 'row' }}>
      <Text style={{ flexGrow: 1 }}>foo</Text>
      <Text style={{ width: '20%' }}>bar</Text>
    </View>
  );
}

to set flexDirection to 'row' to add a horizontal flexbox layout.

We set the 2nd Text component to fill 20% of the screen.

Then we set flexGrow to 1 on the first Text component to expand to fill the remaining width of the screen.

Conclusion

To set width style to a percentage number with React Native, we can use flexbox.

Categories
JavaScript Answers React Native Answers

How to change the height of a FlatList in React Native?

Sometimes, we want to change the height of a FlatList in React Native.

In this article, we’ll look at how to change the height of a FlatList in React Native.

How to change the height of a FlatList in React Native?

To change the height of a FlatList in React Native, we can set the height style of the FlatList.

For instance, we write:

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

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

const Item = ({ title }) => (
  <View
    style={{
      backgroundColor: '#f9c2ff',
      padding: 20,
      marginVertical: 8,
      marginHorizontal: 16,
    }}>
    <Text>{title}</Text>
  </View>
);

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;
  const keyExtractor = (item) => item.id;

  return (
    <View>
      <FlatList
        style={{ height: 300 }}
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
      />
    </View>
  );
}

to set the height of the FlatList to 300px.

Conclusion

To change the height of a FlatList in React Native, we can set the height style of the FlatList.

Categories
JavaScript Answers React Native Answers

How to determine number of lines of Text component with React Native?

Sometimes, we want to determine number of lines of Text component with React Native.

In this article, we’ll look at how to determine number of lines of Text component with React Native.

How to determine number of lines of Text component with React Native?

To determine number of lines of Text component with React Native, we can get it from the onTextLayout callback’s parameter.

For instance, we write:

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

const NUM_OF_LINES = 3;
const SOME_LONG_TEXT_BLOCK =
  'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque pulvinar sem at leo dapibus maximus. Pellentesque nec scelerisque ipsum. Vestibulum sit amet orci vel augue suscipit maximus. Morbi malesuada augue eu sapien iaculis laoreet. Nunc augue purus, ultricies at auctor non, dapibus a ligula. Integer rutrum congue erat a rutrum. ';

export default function App() {
  const [showMore, setShowMore] = React.useState(false);
  const onTextLayout = React.useCallback((e) => {
    setShowMore(e.nativeEvent.lines.length > NUM_OF_LINES);
  }, []);

  return (
    <Text numberOfLines={NUM_OF_LINES} onTextLayout={onTextLayout}>
      {SOME_LONG_TEXT_BLOCK}
    </Text>
  );
}

to set numberOfLines to NUM_OF_LINES to restrict the number of lines to display.

Then we set the onTextLayout prop to the onTextLayout function and get the number of lines displayed with e.nativeEvent.lines.length .

And if e.nativeEvent.lines.length is bigger than NUM_OF_LINES.

We call setShowMore to set showMore to true.

Conclusion

To determine number of lines of Text component with React Native, we can get it from the onTextLayout callback’s parameter.

Categories
JavaScript Answers React Native Answers

How to fix scrollTo is undefined on animated ScrollView with React Native?

Sometimes, we want to fix scrollTo is undefined on animated ScrollView with React Native.

In this article, we’ll look at how to fix scrollTo is undefined on animated ScrollView with React Native.

How to fix scrollTo is undefined on animated ScrollView with React Native?

To fix scrollTo is undefined on animated ScrollView with React Native, we can assign it it a ref.

For instance, we write:

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

const AnimatedScrollView = Animated.createAnimatedComponent(ScrollView);

export default function App() {
  const ref = React.useRef();

  React.useEffect(() => {
    ref.current.scrollTo({ y: 100, animated: true });
  }, []);

  return (
    <View>
      <AnimatedScrollView ref={ref} style={{ height: 200 }}>
        {Array(200)
          .fill()
          .map((_, i) => (
            <Text key={i}>{i}</Text>
          ))}
      </AnimatedScrollView>
    </View>
  );
}

to create an animated scroll view with createAnimatedComponent.

Then we assign a ref to that by calling useRef to create a ref and assign the ref as the value of the ref prop.

Next, we get the value of the ref with ref.current and call scrollTo on it to scroll it.

Conclusion

To fix scrollTo is undefined on animated ScrollView with React Native, we can assign it it a ref.

Categories
JavaScript Answers React Native Answers

How to get the current date in React Native?

Sometimes, we want to get the current date in React Native.

In this article, we’ll look at how to get the current date in React Native.

How to get the current date in React Native?

To get the current date in React Native, we can use the Date constructor.

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>{new Date().toLocaleString()}</Text>
    </View>
  );
}

to create a new Date instance and call toLocaleString on it to return a localized date string and render it in the Text component.

Conclusion

To get the current date in React Native, we can use the Date constructor.