2. What is React
A Javascript library for building user interfaces like web applications.
React can be used as a base in the development of single-page applications.
3. Create and Render a React Element
const element = React.createElement(
'h1',
{
id: 'main-title',
title: 'This is a title'
},
'Hello World'
)
ReactDOM.render(element, document.getElementById('root'));
5. JSX
A syntax extension to Javascript that is used with React to describe elements in
the UI. As we’ll see, it resembles writing HTML.
We need Babel to transpile JSX into Javascript to be rendered on the browser. Try
it out babeljs.io/repl
To add Babel in a project:
<html/><head/> - <script src="https://unpkg.com/babel-
standalone@6.15.0/babel.min.js"></script>
<html/><head/> - <script type="text/babel" src="app.js"></script>
6. Set up a development server
Install http-server
1. npm init -y
2. npm i -S http-server
Add a start script to the package.json
"scripts": {
"start": "http-server"
},
7. Create React Project
create-react-app is an abstract way of getting started on a React Project. It
configures:
- Development server
- Webpack
- Babel
To install via command line/terminal:
npx create-react-app project-name
17. Passing Data to components(props)
import React, { Component, Fragment} from 'react';
class MainBody extends Component {
state = {
value:this.props.value
}
render() {
return (
<Fragment>
...
</Fragment>
);
}
export default Items;
18. Passing Children
Special prop called children, we use when we want to pass something between
the opening and closing tags of an element
1. Pass element in the component
2. Pass{this.props.children}in the parent component
<MainBody><h4>Item #{counter.id}</h4></MainBody>
A single-page application (SPA) is a web application or web site that interacts with the user by dynamically rewriting the current page rather than loading entire new pages from a server. This approach avoids interruption of the user experience between successive pages.
Add react to a website https://reactjs.org/docs/add-react-to-a-website.html
The createElement() method creates and returns a React element of the given type
React doesn’t create actual DOM nodes. React creates objects that describe DOM nodes
The ReactDOM.render() method renders React elements to the DOM
React doesn’t manipulate and update the DOM directly. React only manages what gets rendered to the DOM via ReactDOM.render(). It’s the job of render() to interpret the element objects and create DOM nodes out of them.
Webpack for bundling our files
Babel for compiling our JSX into javascript
Create Navbar and render it in App.js
Arrow functions -> to access this.state.value
Update state -> this.setState({ value: this.state.value + 1 })