Please enable JavaScript.
Coggle requires JavaScript to display documents.
React-JS A JavaScript library to build User Interface - Coggle Diagram
React-JS
- A JavaScript library to build User Interface
Why React ?
- UI State becomes difficult to manage with Vanilla Javascript
- Focus on Business Logic, not on preventing your App from exploding
- Huge Ecosystem, Active Community, High Performance
Content
- Basics
- Styling Components
- Debugging
- Component Deep Dive
- HTTP Requests
- Routing
- Forms & Validation
- Redux
- Authentication
- Testing Introduction
- Deployment
- Bonus( Animations, Next Steps, Webpack)
Basics
Setup
- In your terminal, run commands below, after installing VS Code and npm
-
npx create-react-app my-app
-
cd my-app
-
npm start
Reset
- In the index.css file add the following lines :-
-
*{
-
margin : 0;
-
padding : 0;
-
box-sizing : border-box;
-
}
-
body {
-
overflow-x : hidden;
-
}
-
Components
There are two types of components:
- Functional components (also referred to as "presentational", "dumb" or "stateless" components =>
const cmp = () => { return <div>some JSX</div>
} (using ES6 arrow functions as shown here is recommended but optional)
- class-based components (also referred to as "containers", "smart" or "stateful" components) =>
class Cmp extends Component { render () { return <div>some JSX</div>
} }
Prefer stateless component as often as possible, because we should use state only when needed, as, having state in all the components and manipulating it from anywhere in your app, makes the app unpredictable and hard to manage, especially as it grows.
Data Handling
props
and state
are CORE concepts of React. Actually, only changes in props and/ or state trigger React to re-render your components and potentially update the DOM in the browser
- props: allow you to pass data from a parent (wrapping) component to a child (embedded) component.
- state: Whilst props allow you to pass data down the component tree (and hence trigger an UI update), state is used to change the component, well, state from within. Changes to state also trigger an UI update.
- props.children: you can pass anything between a component,
<Comp>...</Comp>
and access it via props.children inside the Comp component.
Event Handling
You can find a list of supported events here
Function Call
There are two ways to call a function on event handling:
-
this.method.bind(this,arg)
this is more efficient
-
() => this.method(arg)
this is more modern
Two-way Binding
- If you are displaying and changing the state of a component through one of it's child component (passing state as well as changestate through the props) then it is called two-way binding.
-
for eg <input type="text" onChange={props.change} value={props.value}/>
Here you're giving the value to the input as well as passing reference of a method which changes the value.
Styling
- We can also import .css file which is taken care by Webpack(build tool)
- It automatically add everything inside .css file inside the <style> tag in the output HTML file.
Lists and Conditionals
Output List
-
this.state.list.map(element => {
-
return anyJSXelement
-
})
Delete from list
- pass props =>
-
delete = {()=>this.deleteHandler(index)}
deleteHandler = index => {
-
this.setState({list : [...this.state.list].splice(index,1)})
-
Key
While rendering list, provide a key prop with something unique value(don't provide unique value which might change eg index of list -> every DB has a unique id)
-
-
-
Planning a React App
- Component Tree/Componenent Structure
- Application States (Data)
- Components vs Containers
-
-