question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

props not support style like "{width: '100%'}"

See original GitHub issue
import Image from 'react-native-fast-image';
//some code 
<View style={{flex:1}}>
  <Image
       style={{width: '50%'}}
       source={{
          uri: {URI},
          priority: Image.priority.normal,
       }}
       resizeMode={Image.resizeMode.contain}
   />
</View>

prop style={{width: '50%'}} did not work, what should i do to let this style works?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
lpaz010commented, Mar 11, 2019

@JofBigHealth Thanks for the response. I finally found out the way to do like the below, it works!

getHeight = () => {
    if (!this.state.height) return Image.defaultHeight;
    if (!isNaN(this.props.width)) {
      const ratio = this.state.height / this.state.width;
      const height = this.props.width * ratio;
      return height;
    }else{
      return Image.defaultHeight;
    }
  }

render() {
    const height = this.getHeight();
    return (
      <FastImage
        {...this.props}
        onLoad={this.onLoad}
        style={[{ width: this.props.width, height }, this.props.style]}
      />
    )
  }

Header Appreciate the response and apologize for not responding in time. i will close this issue Thanks again.

1reaction
24devcommented, Apr 25, 2019

I found a similar fix to @lpaz010, like this:

handleLoad = (tempWidth, tempHeight) => {
    const { width } = this.props;
    const ratio = tempHeight / tempWidth;
    const height = width * ratio;
    this.setState({
      height,
    });
  };

render() {
    const { width } = this.props;
    const { height } = this.state;

    return (
       <FastImage
              style={{width,height, }}
              source={{uri,}}
              onLoad={e => this.handleLoad(e.nativeEvent.width, e.nativeEvent.height )}
              resizeMode={FastImage.resizeMode.cover}
        />
    )
}

It works like react-native-scalable-image, which I was using before fast-image. Send the width you want in the props of the component, e.g. device width.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set image width to be 100% and height to be auto in ...
Image's height won't become automatically because Image component is required both width and height in style props . So you can calculate by...
Read more >
width - CSS: Cascading Style Sheets - MDN Web Docs
The width CSS property sets an element's width. By default, it sets the width of the content area, but if box-sizing is set...
Read more >
Layout Props - React Native
It works similarly to max-width in CSS, but in React Native you must use points or percentages. Ems and other units are not...
Read more >
How To Style React Components | DigitalOcean
In this tutorial, you'll learn three different ways to style React components: plain Cascading ... Here, you gave the <h2> a width of...
Read more >
How To Use Styled-Components In React - Smashing Magazine
Apart from helping you to scope styles, styled components include the ... already set a default for it with width: props.width || "100%",...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found