1

Big fan of inline styles and decided to give it a try. I'm slowly getting the hang of it but I'm now stuck because I keep getting the "Unknown prop styles on <img> tag" error.

My code is as shown below:

render(){
    let imgUrl = 'http://mrmrs.io/images/0006.jpg';
    let divStyles = {
        backgroundImage:'url(" + imgUrl + ")',
        backgroundSize: 'cover'
    };
    return(
        <main class="cf w-100">
          <div class="fl w-50 w-third-m w-25-ns">
            <div class="aspect-ratio aspect-ratio--1x1">
              <img styles="{{divStyles}}"  class="db bg-center cover aspect-ratio--object" />
            </div>
          </div>
        </main>

    )
}

The simple stuff are always the ones that give me the most problems. Any help would be appreciated.

4
  • Shouldn't you use className instead of class when writing JSX? Commented Oct 25, 2016 at 5:41
  • change class to className and also when the divStyles is an object just use it as style={divStyles} Commented Oct 25, 2016 at 5:44
  • Thanks guys. I tried the above but now get "The style prop expects a mapping from style properties to values, not a string. For example, style={{marginRight: spacing + 'em'}} when using JSX." Commented Oct 25, 2016 at 6:34
  • Do I see correctly that you are defining an <img> without a src but with only a background-image? If so, you're doing it wrong. Commented Nov 6, 2016 at 8:58

3 Answers 3

3

The prop should be style not styles. And you need to define a height and a width to the img tag.

Hope this helps!

class App extends React.Component{
  render(){
      let imgUrl = 'http://mrmrs.io/images/0006.jpg';
      let divStyles = {
          backgroundImage:'url(' + imgUrl + ')',
          backgroundSize: 'cover',
          height: 200,
          width: 200,
      };
      return(
          <main className="cf w-100">
            <div className="fl w-50 w-third-m w-25-ns">
              <div className="aspect-ratio aspect-ratio--1x1">
                <img style={divStyles}  className="db bg-center cover aspect-ratio--object" />
              </div>
            </div>
          </main>

      )
  }
}
ReactDOM.render(<App/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I had to change backgroundImage:'url(" + imgUrl + ")', to backgroundImage:'url(' + imgUrl + ')', and change styles to style and it worked.
@user3590911 That's goood!
0

Firstly you need to use the style object differently like style={divStyles} and not styles="{{divStyles}}" second you image will now be rendered since 'url("+ imgUrl + ")', will be treated as a string and not rendered as 'url("http://mrmrs.io/images/0006.jpg")',

class App extends React.Component {
  render(){
    let imgUrl = 'http://mrmrs.io/images/0006.jpg';
    var url = 'url("' + imgUrl + '")'; 
    let divStyles = {
        backgroundImage:url,
        backgroundSize: 'cover'
    };
    return(
        <main class="cf w-100">
          <div class="fl w-50 w-third-m w-25-ns">
            <div class="aspect-ratio aspect-ratio--1x1">
              <img style={divStyles}  class="db bg-center cover aspect-ratio--object" />
            </div>
          </div>
        </main>

    )
}
}

ReactDOM.render(<App/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
<div id="app"></div>

Comments

0

You are confused with {{}} and {} in React.

With your code, it is not required to have double braces {{}} because the divStyles is already an object.

Try the below one, should work.

<img styles="{divStyles}"  class="db bg-center cover aspect-ratio--object" />

The above could be written as shown below

<img styles="{{
      backgroundImage:'url(' + imgUrl + ')',
      backgroundSize: 'cover',
      height: 200,
      width: 200,
  }}"  class="db bg-center cover aspect-ratio--object" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.