# What is React ?
React is a JavaScript library. NOT A FRAMEWORK. For building web-apps using components.
# Basic Understanding
# jsx
React uses a syntax extension .jsx
(JavaScript XML) of JavaScript.
We use this extension when mixing HTML & JS code.
# DOM - Document Object Model
Can be though that every HTML-Element is a element of the DOM.
When working with React, it creates a virtual DOM. In React, the DOM is updated automatically without a need for a refresh. As we can see in the “Real DOM”:
# File- & Folder-Structure
- node_modules: external packages, libraries the project depends on
- public: contains any public assets (fonts, images, videos, …)
- src>assets - files are bundled and automatically refreshed in the app
- src: main.jsx (main .js file), App.jsx (root-component)
- assets: files, videos, …
- index.html - main entrypoint of application (script: main.jsx)
- package.json - contains metadata about project
# Components
Here we gonna create a first component. This component will be exportet in our root-component.
# JavaScript & HTML ?
To write you JS-code mixed up with HTML you have to use { your js-code}.
# jsx fragement
As you know you can only return one elemente in a .jsx file. Therefore, we need a workaround.
We gonna use jsx fragements.
With these changes, we are able to add multiple components into one component.
# props
These are read-only properties, that are shared between components. A parent component can share data to a child component.
# proptypes
This is a mechanism, that ensures that the passed value is of the correct datatype. age: PropTypes.number
# defaultProps
Specify a default value for props in case they are not passed from the parent component. name: “Guest”
# Conditional Rendering
This allows you to control what gets rendered in your application based on certain conditions (show, hide, change components).
We can resolve this using booleans & then displaying components.
# Click-Event
An interaction when a user clicks on a specific element. We can respond to clicks by passing a callback to the onClick event handler.
# React hook
Special function, that allows functional components to use React features without writing class components.
- useState, useEffect, useContext, useReducer, useCallback, …
If a function begins with an “use” it probably is a React hook.
# useState()
This is a React hook, that allows the creation of a stateful variable AND a setter function to update its value in the virtual DOM.
”name, setName”
In this example, we have an array with 2 variables. The name with the current state “name” and a “setName” to change the state/value, the “setName” will automatically transfer the text to the “name” variable.
# onChange
Represents an event-handler primarly used with form elements.
- input, textarea, select, radio, …
Triggers a function every time the value of the input changes.
# Update Function
A function passed as an argument to setState() usually.
! This allows for safe updates based on the previous state !
Used with multiple state updates & asynchronous functions. Good practice to use updater functions.
When using the updater-function, it takes the PENDING state to calculate the NEXT state. React puts your updater-function in a queue. During the next render, it will call them in the same order.