2. Component Based
2. Component Based
3. Suitable for both web and mobile apps
4. Small learning curve
Install node (Node >= 10.16 and npm >= 5.6)
https://nodejs.org/en/download/package-manager/
Install yarn
https://classic.yarnpkg.com/en/docs/install
npm install --global yarn
npm install -g create-react-app
yarn create-react-app introduction-to-react
npx create-react-app introduction-to-react
Recommended
const name = "Rosy"
const course = "React"
const element =
<div className="introduction">
Ola! I am {name} and I am learning {course} for {5*4} minutes
</div>
const name = "Rosy"
const course = "React"
const element =
<div className="introduction">
Ola! I am {name} and I am learning {course} for {5*4} minutes
</div>
must return a single element (alternatively use, <React.Fragment> </React.Fragment> or <> </>)
<>
<h1>blah</h1>
<span>what</span>
<p>ola</p>
</>
const name = "Rosy"
const course = "React"
const element =
<div className="introduction">
Ola! I am {name} and I am learning {course} for {5*4} minutes
</div>
use className instead of class
camelCase attributes (onClick, onSubmit)
use curly branches for expressions
const name = "Rosy";
const course = "React";
const element =
/*#__PURE__*/
React.createElement(
"div",
{
className: "introduction"
},
"Ola! I am ", name, " and I am learning ", course, " for ", 5 * 4, " minutes"
);
//Syntax
React.createElement(component, props, ...children)
const element = {
type: 'div',
props: {
className: 'introduction',
children: "Ola! I am ", name, " and I am learning ", course, " for ", 5 * 4, " minutes"
}
};
const name = "Rosy"
const course = "React"
const element =
<div className="introduction">
Ola! I am {name} and I am learning {course} for {5*4} minutes
</div>
//syntax
ReactDOM.render(componentToRender, targetNode)
State changes
Creates a VDOM with new update
Compares with previous VDOM state
Change in state
Updates only those changes in DOM
Mounting
when component renders for the first time
Mounting
Mounting
Mounting
Updating
update existing nodes in the DOM
Updating
Updating
componentDidUpdate(prevProps, prevState) {
const currentProps = this.props
if (currentProps.name !== prevProps.name) {
// do something
}
}
Unmounting
Unmounting
npm i react-router-dom
yarn add react-router-dom
function Routes(){
return (
<BrowserRouter>
<Switch>
<Route path={'/main'} component={Main}>
<Route path={'/profile'} component={Profile}>
</Switch>
</BrowserRouter>
)
}