saritha
saritha

Reputation: 659

How to use import statement in react?

how to use import statement to import functions from one component to other component.

Below is how the import statement is:

import Tool from '../Common';
import { ToolContextProvider } from '../Common';

This complaint of duplicate lines. So I have tried something like below,

import { ToolContextProvider, Tool} from '../Common';

But this doesn't seem to be correct. How can write this in one line. Could someone help me with this? Thanks.

Upvotes: 1

Views: 788

Answers (2)

amdev
amdev

Reputation: 7460

basically there are two different type of export in javascript modules (also react included):

  1. default export
  2. named export

default export would be like :


// someFile.js
export default SomeComponent

named export would be like


// someFile.js
export SomeOtherComponent

importing them in other components for using them should be like:

// useCase.js
import SomeComponent from './someFile' // for default export 
import { SomeOtherComponent } from './someFile' // for named export 
import SomeComponent, { SomeOtherComponent } from './someFile' // for using both

Upvotes: 4

Surjeet Bhadauriya
Surjeet Bhadauriya

Reputation: 7166

You can import like this. Just combine both of them.

import Tool, { ToolContextProvider } from '../Common';

Upvotes: 4

Related Questions