Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The `style` prop expects a mapping from style properties to values, not a string

import React, { Component } from 'react';

class App extends Component {
  render() {
  const name = 'Red Header';
  const styleRed = {backgroundColor : 'red'};
  return (
    <div style="{styleRed}">
      {name}
    </div>
  );
 }
}

export default App;

That is my code I write the style object properly, but react still arguing. Why this happens It says :

'For example, style={{marginRight: spacing + 'em'}} when using JSX.'.

My code looks like the one above, but react argues.

like image 259
Osman Omar Avatar asked Mar 05 '23 04:03

Osman Omar


1 Answers

Try removing "" from the style props. https://codesandbox.io/s/6j91o6z56w

import React, { Component } from 'react';

class App extends Component {
  render() {
  const name = 'Red Header';
  const styleRed = {backgroundColor : 'red'};
  return (
    <div style={styleRed}>
      {name}
    </div>
  );
 }
}
like image 77
Ahsan Sohail Avatar answered May 01 '23 15:05

Ahsan Sohail