2

I'm porting my code from ES6 to .TS / .TSX

Given a folder structure like this:

Table/Tables.tsx

Importing from another react component in ES6 I used to be able to just do this:

import Table from '../Table'

i.e. it would be smart enough to resolve the import by folder name. Now that I've switched to Typescript I have to import more verbosely (from another .tsx file);

import Table from '../Table/Table'

I've looked here https://www.typescriptlang.org/docs/handbook/module-resolution.html

I'm using Webpack with React Starter Kit.

2 Answers 2

3

Inside each folder you wan to import, have an index.ts file with all exports of that folder. Then you will be able to import individual components by only specifying folder path

import {table} from ../table/

Sign up to request clarification or add additional context in comments.

1 Comment

ok that's helpful, I was trying to avoid having another file as I am following the one folder one ocmponent convention, so I could just rename my component to index.ts in this case.
2

I actually found the same question, asked a bit differently here

The package.json modification didn't work for me, so I just changed my convention to naming the component Index.tsx in each subfolder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.