Estoy tratando de establecer estilos en línea en mi aplicación React. En este caso, para un span:
<span className="myClass" style={{float : 'left', paddingRight : '5px'}} > </span>
React me dice:
Uncaught Invariant Violation: The style
prop expects a mapping from
propiedades de estilo a valores, no una cadena. Por ejemplo,
style={{marginRight: espaciado + 'em'}} cuando se usa JSX. Este nodo DOM
fue renderizado por `SentenceView
No estoy muy seguro de lo que significa.
¡PS: He probado diferentes versiones, así que hice paddingRight: 5
, así como paddingRight: 5 + 'px'
, así como paddingRight : 5px
, pero no tuve ningún éxito!
Hay algunas maneras de establecer el estilo de React Components.
https://facebook.github.io/react/docs/context.html
https://github.com/facebookincubator/create-react-app
utilizando className="your-class-name"
.
utilizando style={css_object}
o style={{color: this.props.color}}
.
stylesheet.css
begin snippet: js hide: false console: true babel: false -->
import './styles.css';
/*
.classname-color{
color: "red";
background: "#0f0";
}
*/
const BTN = (props) => {
return (
<div>
My name is <button>{props.name}</button>
<hr/>
I'm <span className="classname-color">{props.age}</span> yeas old!
</div>
);
};
const infos = {
name: "xgqfrms",
age: 23
};
ReactDOM.render(<BTN {...infos} />, mountNode);
.classname-color{
color: "red";
background: "#0f0";
}
Fin del fragmento;
begin snippet: js hide: false console: true babel: false -->
// <span style={styles}>
const styles = {
color: "red",
background: "#0f0",
fontSize: "32px"
};
const BTN = (props) => {
return (
<div>
My name is <button>{props.name}</button>
<hr/>
I'm <span style={styles}>{props.age}</span> yeas old!
</div>
);
};
const infos = {
name: "xgqfrms",
age: 23
};
ReactDOM.render(<BTN {...infos} />, mountNode);
// <span style={{color: styles.color}}>
const styles = {
color: "red",
background: "#0f0",
fontSize: "32px"
};
const BTN = (props) => {
return (
<div>
My name is <button>{props.name}</button>
<hr/>
I'm <span style={{color: styles.color}}>{props.age}</span> yeas old!
</div>
);
};
const infos = {
name: "xgqfrms",
age: 23
};
ReactDOM.render(<BTN {...infos} />, mountNode);
Así es como se puede definir y utilizar el estilo inline con react.
/**
* Style definitions.
*/
const STYLE = {
infoColor: {
color: 'green'
},
warningColor: {
color: 'orange'
},
errorColor: {
color: 'red'
}
};
/**
* Component
*/
class Welcome extends React.Component {
/**
* Rendering into the DOM.
*/
render() {
return (
<div>
<h2 style={STYLE.infoColor}>Welcome!</h2>
)
}
}