Skip to content

gitrhs/react-native-auto-height

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

react-native-auto-height

this code aims to set image height to auto

Step by Step

  1. create images variable to store the image
const images = [
  'URL1', //image 1
  'URL2', //image 2
  //add another image here
];
  1. create aspect ratio variable

Set aspectRatio = 1 (1:1), it will be the temporary aspect ratio before the real value counted

const [aspectRatios, setAspectRatios] = useState(images.map(() => 1));
  1. Find the real aspect ratio with useEffect
useEffect(() => {
  Promise.all(images.map(imageUrl => new Promise((resolve, reject) => {
    Image.getSize(imageUrl, (width, height) => {
      resolve(width / height);
    }, reject);
  }))).then(ratios => {
    setAspectRatios(ratios);
  });
}, []);
  1. Return the Image
{images.map((imageUrl, index) => (
  <View key={index}>
    <Image
      style={{
        width: '100%',
        aspectRatio: aspectRatios[index],
      }}
      resizeMode={'contain'}
      source={{ uri: imageUrl }}
    />
  </View>
))}

Full Code Example:

import React, { useState, useEffect } from 'react';
const autoResize = () => {
  const images = [
      'URL1', //image 1
      'URL2', //image 2
      //add another image here
    ];
  const [aspectRatios, setAspectRatios] = useState(images.map(() => 1));
  useEffect(() => {
    Promise.all(images.map(imageUrl => new Promise((resolve, reject) => {
      Image.getSize(imageUrl, (width, height) => {
        resolve(width / height);
      }, reject);
    }))).then(ratios => {
      setAspectRatios(ratios);
    });
   }, []);
  return (
    {images.map((imageUrl, index) => (
     <View key={index}>
       <Image
         style={{
           width: '100%',
           aspectRatio: aspectRatios[index],
         }}
         resizeMode={'contain'}
         source={{ uri: imageUrl }}
       />
     </View>
   ))}
  );
};

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published