Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
Javascript reactJs css in javascript in external file

I am using reactjs and trying to get me css inside javascript and finally read the css from an external js file.

Here is the code:

import React from 'react';
import ReactDOM from 'react-dom';

var styles = {
  container: {
    padding: 20,
    border: '5px solid green',
    borderRadius: 2
  }
};

var myComponent = React.createClass({
  render: function() {
    return (
      <div style={styles.container}>
        {this.props.name}
      </div>
    );
  }
});

This works fine but what I need to do is to put the css in an external file. So I've created a file called general.css.js

And I tried to import it:

import styles from './components/general.css';

I add this import to the top of the page with the other imports.

The problem is that it's not reading the styles.

What I'm I doing wrong here?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Make a new file and put this code in it.

export const style = { container : { 
  padding: 20,
  border: '5px solid green', 
  borderRadius: 2 } 
};

Now in your component file.

import * as styles from './style/location/filename'

Now you can use styles in your render function.

return (
    <div style={styles.style.main}>
      <h3 style={styles.style.header}>Vote for your favorite hack day idea</h3>
    </div>
  );
over 4 years ago · Santiago Trujillo Denunciar

0

You can directly import your css file in js.

import './style/app.css';

app.css

.page { background-color:#fafafa; }

and you can use this class in React component like below.

<div className="page">

Hope it works!!!!

over 4 years ago · Santiago Trujillo Denunciar

0

There is a little tool that automates translation from CSS to JSON representation.

Worth checking that out.

Note how the translation adds _ underscores:

div.redcolor { color:red; }
div:hover { color:blue; }

Into:

{"div_redcolor":{"color":"red"},"div_hover":{"color":"blue"}}

Note how the Vishwas Chauhan used starred method of ES6 import export:

enter image description here

In case you use this tool you get one big object, and you can use any method.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda