{name}
{price}
{description}
React is a declarative JavaScript library for building user interfaces specifically for single-page applications (SPA).
It is open source and created by facebook (meta) in 2013.
Declarative: Tell me what you want to do
Imperative: Tell me how to do it
Single-page application is a web app implementation that loads only a single web document, and then updates the body content of that single document via JavaScript.
No refresh required for navigation!
Here is an example
Components are the building blocks of any React app.
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.
Let check Snapp's components
We have two types of components
class Button {
render(){
return (
);
}
}
function Button(){
return (
);
}
A component is a function that returns HTML or a javascript class that returns HTML inside it's render method
We can have something like this
function Button(){
const text = 'Click me!!!';
return (
);
}
Or something like this
function Button(){
return (
{
isLoading? () : ();
}
);
}
We can start writing javascript inside JSX inside { }
function Button(){
const text = 'Click me!!!';
return (
);
}
function Date(){
return (
We're in {new Date().getFullYear()}
{/* We're in 2021 */}
);
}
Composing Components
function HomePage(){
return (
...
);
}
We can render conditionally
function NotificationButton(){
return (
{hasNewNotification && ( )}
);
}
Is this component reusable?
function Button(){
return (
);
}
What if we want a button with 'submit' text?
function ClickMeButton(){
return (
);
}
function SubmitButton(){
return (
);
}
How can we make this component reusable?
Props are arguments passed into React components.
Define props and pass them down to components
Render prop inside the component
function Button(props){
return (
)
}
Components can have multiple prop
function Coffee({name, img, description, price}){
return (
{name}
{price}
{description}
);
}
React components can have internal state
State is like a component's memory
When the state changes, react will update the UI automatically (re-render)
Lets implement a counter component and see the state in action
function Counter(){
let counterValue = 0;
const handleIncrementClick = () => counterValue ++;
return (
Counter value is: {counterValue}
)
}
Clicking the button will increase the counter value
But it wont update the UI
Now, we can use state
We can define state with
const [value, setValue] = useState(initialValue);
// value is the actual state.
// setValue is a special function to update the value.
// By Calling setValue(newValue), state and UI will be updated.
// You can name them whatever you want.
// You can have as many state as you want (multiple useState call).
Lets define the state
function Counter(){
const [value, setValue] = useState(0);
const handleIncrementClick = () => counterValue ++;
return (
Counter value is: {value}
)
}
Lets see component styling in action
Lets see fetching external data in action