JavaScript import all variables from a file.js with an “ import './file.js' ” summoning import variables from another file python,import variables from another

I'm working with the import function, I wish use an import like for css, I mean "import './file.css'" then all the css attributes are diffused in the file. I have tried the same with ReactJS but it fails.

My expectation is to imitate the css import for js files, but it doesn't work.

Here my sandbox

Here is the relevant code:

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

impoprt sample from "./sample" import "./exported.js"; import "./styles.css";

function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      {sample[2]}
      {text1}
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

I'm getting the error at the line when no using the star's import:

  {text1}

I'm wondering how make something similar. Any hint would be great,

thanks

Answer:1

The offending code in your sandbox is: import "./exported.js";

One source of confusion is that you are using Create React App, which hides away the webpack magic which allows you to import your CSS files as import "./styles.css";. This is not how module exports and imports work. I would recommend reading the section on exporting and importing details on exploringjs.com

What you are doing is essentially an empty import, i.e. you are not importing anything, just executing the file.

Empty import: only loads the module, doesn’t import anything. The first such import in a program executes the body of the module.
import 'src/my_lib';

But here are various ways to import something in a nutshell.

Assumption: your ./exported.js file has the following exports:

// some other code
export { text1, text2 };
export default config;

Then you can import them in various formats

// import only the default export
import config from './exported.js';
// This only imports the export qualified with default, it ignores others 
// i.e. 
console.log(config); //works
console.log(text1); // fails
console.log(text2); // fails

// import everything the module exports, but as a namespace
import * as myLib from './exported.js';
// usage: all named exports are properties of the myLib object
console.log(myLib.text1); // works
console.log(myLib.text2); // works
console.log(myLib.config); // should not work, unless you have also exported config as a named export

// import only what you need
  import { text1, text2 } from './exported.js';
  console.log(text1); // works
  console.log(text2); // works

// you can also rename them
  import { default as x, text1 as a, text2 as b } from './exported.js';
  console.log(x); // works --> config
  console.log(a); // works --> text1
  console.log(b); // works --> text2
Answer:2

I face difficulties to write reducers for Redux. Specifically I end up wrapping a lot of my state manipulation in .map, .slice etc. When the structure of my state object grows larger, the reducers ...

I face difficulties to write reducers for Redux. Specifically I end up wrapping a lot of my state manipulation in .map, .slice etc. When the structure of my state object grows larger, the reducers ...

I have a Rails 4.2 app that is getting requests from another domain. I'd like them to send a uuid that we set in our javascript. It works on localhost but not cross-domain. In our ...

I have a Rails 4.2 app that is getting requests from another domain. I'd like them to send a uuid that we set in our javascript. It works on localhost but not cross-domain. In our ...

  1. rails cross domain cookies
  2. rails cross domain
  3. rails allow cross domain requests
  4. rails api cross domain

I am retrieving a document from PouchDB in an Angular Service. The document is retrieved in the following format: { "_id":"segments", "_rev":"1-4f0ed65cde23fe724db13bea1ae3bb13", "segments":[ { "...

I am retrieving a document from PouchDB in an Angular Service. The document is retrieved in the following format: { "_id":"segments", "_rev":"1-4f0ed65cde23fe724db13bea1ae3bb13", "segments":[ { "...

  1. rxjs json array
  2. rxjs map json array

I'm having trouble figuring this out. I've created a table, and I want to use clickable buttons (not the arrow keys) to navigate through each cell and mark that cell as yellow. The table builds and I ...

I'm having trouble figuring this out. I've created a table, and I want to use clickable buttons (not the arrow keys) to navigate through each cell and mark that cell as yellow. The table builds and I ...

  1. javascript using buttons
  2. javascript using radio buttons
  3. javascript calculator using radio buttons
  4. calculator program in javascript using buttons