Would it be possible to import * from multiple files within the same import declaration i.e
Imports would usually be made with one file as follows
import * as action from '../actions/ActionCreators'
Would the following be possible by some means?
import * as action from { '../firstActions/FirstActionCreators', '../secondActions/SecondActionCreators' }
whereby the same is import is made from two different directories/files.
Thanks
The best practice is to import only the required stuff. Since you are using ES6 you can try like this.
import { Router, Route, Link, browserHistory,IndexRoute } from 'react-router'
Related
Today I am using import aliasing to change the name of an import in React:
import { Something as SomethingElse } from 'somewhere';
However, after switching this file over to TypeScript, the same thing doesn't seem possible. Only after removing the alias am I able to use the import:
import Something from 'somewhere';
Is it possible to use an import alias in TypeScript?
That's a default export, so you could just name it whatever you want:
import SomethingElse from 'somewhere';
Although... changing the file to TypeScript should not magically cause it to change from named export to default export...
When we write code in index.js file in src folder of an React app first of all we write this line:
import React from 'react';
I know react is a package
But I want to know what is React basically
an object, a method or something else.
The React variable that you import is an object, and it contains most of the methods that are used by React when generating a web-page.
The reason this import has been historically required is because the JSX that you write (e.g. return <p>text</p>) gets converted into a function call, calling the React.createElement function.
Note that in newer versions of React you no longer need to import react when using JSX. See https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html for more information.
Credit to Olivier Boissé for an answer in the comments.
In order to find out what React is, you just need to write:
console.log(react);
Then you will see that it is an object with many properties and methods.
String:
import React from "react";
We are writing in order to import this object into a file where we will use some of its method. If you don't use anything from React in the file, then you don't need to import it.
For example, in react 18, it is no longer necessary to import the react object into a file index.js
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(<App />);
In any case, at this stage of your study of react, it may not be entirely clear to you what is used in it and for what, but in the future you will become more aware of all the possibilities of react. There is a time for everything
I have a custom React Hooks, named bar at foo directory, and I import it with the way below:
import * from "../../foo/bar";
and the question is, how can I do it with the way:
import * from "#foo/bar";
to avoid find the directory boring.
React error message wont construct my code
I'am confused on the pathway and terminal .....
Ive already tried changing the name of my app and that doesn't work
https://gist.github.com/patrowheels/fece9597d6862094a1a3672b9d6cdecf
You are importing as:
import Header from "./components/Header"
import MainContent from "./components/MainContent"
import Footer from "./components/Footer"
Yet, you say your file names are header.js, footer.js, etc.
Try importing as:
import Header from "./components/header.js"
import MainContent from "./components/maincontent.js"
import Footer from "./components/footer.js"
so the filenames match.
Say I have the following:
import validator from 'validator'
and in my code do:
validator.isEmail(txt)
Would that import the entire validator and increase the overall package size?
if, how do I avoid this?
Your statement will import the whole validator.
What are you looking for is this
import { isEmail } from ‘validator’
isEmail should be e exported in the source file.