Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native image with headers

Tags:

react-native

How to pass headers into Image element?

This approach does not fit due to impossibility to cache images and base64 conversion will be hard with small RN Js-thread
https://stackoverflow.com/a/36678631/6119618

This is not working at all on neither android nor ios
https://stackoverflow.com/a/44713741/6119618

I don't want yet eject my project, so modules like FastImage and others using react-native link will not work

UPD
tried to launch the same app on ios and images aren't showing up. Android works

like image 826
IC_ Avatar asked Aug 23 '17 01:08

IC_


People also ask

How do you add a header in React Native?

To configure the header bar of a React Native application, the navigation options are used. The navigation options are a static property of the screen component which is either an object or a function. headerTitle: It is used to set the title of the active screen. headerStyle: It is used to add style to the header bar.

How do I display images in React Native?

Adding Image Let us create a new folder img inside the src folder. We will add our image (myImage. png) inside this folder. We will show images on the home screen.

How do I display an image from URL in React Native?

So here is the solution to insert an image from an external URL in your application: //MyComponent. js import React from 'react'; import { Image } from 'react-native'; const imageUrl = "https://images.unsplash.com/photo-1526045612212-70caf35c14df"; export class MyComponent extends React.


1 Answers

The better approach would be something like this.

import { Image } from 'react-native';

<Image 
  source={{ 
    uri: 'source uri',
    headers: {
      ...headers
    }
  }}
/>

Above method would work for almost every case, However you can also separately fetch the image using fetch api or axios (pass the headers in the request). Then store the base64 in state and then use that as uri.

like image 106
Vikas chhabra Avatar answered Sep 29 '22 17:09

Vikas chhabra