Create your first component in SolidJS

I will help you to create your first component with this guide. This guide also assume you were familiar with Typescript.
- We have a new basic solidjs project with following folder structure.
1 2 3 4 5 6 7 8 9 10 11 12 13
. ├── public │ ├── index.html │ └── manifest.json ├── src │ ├── index.js │ ├── index.css │ ├── App.tsx │ ├── App.test.tsx │ └── App.css ├── package.json ├── tsconfig.json └── README.md
- Create a new folder which name
components
undersrc
. This folder will contain all your components.1 2 3 4 5 6 7 8 9 10 11 12 13 14
. ├── public/ │ ├── index.html │ └── manifest.json ├── src/ │ ├── components/ │ ├── index.js │ ├── index.css │ ├── App.tsx │ ├── App.test.tsx │ └── App.css ├── package.json ├── tsconfig.json └── README.md
- Create a new
HelloWorld.tsx
file incomponents
folder.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
. ├── public/ │ ├── index.html │ └── manifest.json ├── src/ │ ├── components/ │ └── HelloWorld.tsx │ ├── index.js │ ├── index.css │ ├── App.tsx │ ├── App.test.tsx │ └── App.css ├── package.json ├── tsconfig.json └── README.md
- We declare a new HelloWorld component in
HelloWorld.tsx
as following.1 2 3 4 5 6
function HelloWorld() { return ( <h2>Hello World!</h2> ); } export default HelloWorld;
- Update your
App.tsx
file.1 2 3 4 5 6 7
import HelloWorld from './components/HelloWorld'; function App() { return ( <HelloWorld></HelloWorld> ); } export default App;
Note: your custom component name should use uppercase to distinguish from native HTML tag.