Project WebApp — From code to UI - Introduction to React.JS
Author
KalleDate Published
To use react we need two packages from npm, react itself and react-dom. React takes care of managing a virtual DOM representation and is highly optimized to handle state updates and changes in the virtual DOM tree following the state change. The second part "react-dom" is responsible for bringing the virtual representation of the DOM to the browser and also provides wrapped APIs for browser based stuff like event-handlers on elements.
Because every new beginning starts with a hello-world, here it is in react:
As you can see there is some HTML-like syntax mixed right into the JavaScript-Code, this language extension is called JSX. This extended syntax enables you to write shorter and more readable code, but it must be transpiled to real JavaScript before it can be executed in a browser.
The hello-world as transpiled code looks like this:
So every JSX-Tag gets replaced with a call to React.createElement(tag, props, children)
. That's already a nice to have feature, but JSX starts to shine when you define your own UI components and can use them as new JSX-tags. Components can either be classes extending the React.Component class, or simple functions returning valid JSX elements. There is only one convention based rule, the name of the Component either class or function must start with a capital letter, because all JSX-tags starting with a lowercase letter are interpreted as native browser elements. Now a hello-world with a self defined component:
-->
For a better understanding of the JSX syntax and its corresponding JavaScript code the babel REPL is a great tool tp play with.
The HelloWorld
component didn't have any parameters but as part as you can see in the React.createElement
, there is second parameter null
. If you want to provide some information to a component you can define and use the properties like this:
-->
The same component defined as a Component-class looks like this:
But Component-classes are only necessary if you have to manage some state inside of the component, and even this can be done with components providing state-management functions.
The last important bit of information about the JSX syntax is the children
property. With children
you get access to the information given to you between the opening and closing tag of your component. So you could redefine the HelloName
component like this:
But with children you provide other JSX-elements so you could also write:
So that's it, now you know the most important things about JSX and can start to use react.
Tasks
- work through the very fine react tutorial
- when finished with the tic-tac-toe game change the css rules to use flex-box and check if the react-layout code can be simplified
Project WebApp — How to style react
Learn how to style React components using various approaches, including inline styles, styled-components, glamorous, and emotion. This guide explains each method's benefits and provides practical examples to help you implement component-based styling efficiently in your React applications.
Mailing List
If you want to receive updates on new posts, leave your email below.