I have two files for my component:
-
app/components/SomeButton.ts
-
app/components/SomeButton.tsx
The .ts
contains most of the logic/code and the .tsx
extends the .ts
and contains just the render function.
When I want to import the .tsx
in some other file (e.g. App.tsx
) like this:
import SomeButton from 'app/components/SomeButton';
it tries to import the .ts
file instead.
Is there a way to force it to load the .tsx
file instead?
A workaround would be to use 2 different names but having te same name would be a bit cleaner.
2
Answers
Thanks to the comment of @DomA I now ended up with this pretty satisfactory construction (though not a perfect answer to the question):
app/components/SomeButton.ts:
app/components/SomeButton.r.tsx: (the
r
is arbitrary)You can now import the component wherever you like like this: import SomeButton from 'app/components/SomeButton.r';
Why not simply add the desired extension to your import:
Although, if I were to split a component into multiple files, I would probably create a folder for that component and keep all the files in there. You could have:
That way, you’re original objective could be accomplished by adding an
index.js
that exports the tsx component as default: