--- type: video title: 'Creating Components in React' description: 'Learn how to Creating Components in React' slug: 'creating-components-in-react' image: name: 'creating-components-in-react.png' videoId: PbkwqVZsUgs status: 'published' date: '2022-09-10' tags: ['nodejs', 'react', 'react js', 'javascript'] --- Start by making a new react app using vite. For this, make sure you've got yarn installed. You can do this with ``` npm i -g yarn ``` Then run ``` yarn create vite ``` Select `react`, you could also selected `react-ts` if you want to try out using typescript. Once you've got your app created, cd into it and run ``` yarn ``` To install dependencies. To run the dev server, use the following command: ``` yarn dev ``` Now we've got all that **placeholder** code inside of **App.jsx**, let's delete it all. Also, delete everything in App.css except for `#root` . App.jsx is the entry point to the application, so try adding some HTML. ```js function App() { return (

Hello

) } export default App ``` ## JSX ### The rules of jsx... and tsx * Return a single root element, div or empty tag `<>` * All tags must close, so you can `` or self close tags like `` * This is js, not html, so any reserved word in JavaScript has a different name here like `className` instead of `class` ```js return (

Hello

World

random
) ``` ## CSS in JS There are countless ways of using CSS in a react project. And each way deserves it's own video, so for now, in the beginning, we'll keep it simple. When you want to add a style add a classname, then add a style ```js
``` And since this is in App.js, we can add the style to App.css ```js .someName { color: red; } ``` ## JavaScript in JSX So like I said, this is js, so you can inject js into jsx. **Not rendered as JS:**

6 + 9

**Rendered as JS:**

{6 + 9}

Try adding more JS things to your markup: ```js return (

Hello

World

{new Date().toString()}

) ``` or A component couples the UI logic and markup together in the same file. So instead of separating your js and html into separate files, it all goes in the same file and we split the app into separate components instead. **Rendering logic and markup live together in the same place—components.** ## More components and Child components If we want to create another component, we can just create a new function with an uppercase character that returns markup: ```jsx function SomeNewComponent() { return (

Some Markup

) } ``` Usually we put each component in their own file, even if they're very small like this one, so create a new file with the same name as the component and put it in there: ``` SomeNewComponent.jsx ``` `import` and `export` using esmodules synax: **SomeNewComponent.jsx** ```jsx export default function SomeNewComponent() { return (

Some Markup

) } ``` **App.jsx** ```jsx import SomeNewComponent from './SomeNewComponent.jsx' ``` Then include it in the markup: ```jsx function App() { return (

Hello

World

{new Date().toString()}

) } ```