Please enable JavaScript.
Coggle requires JavaScript to display documents.
React-JS - Coggle Diagram
React-JS
Theory
- 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
-
-
Redux
Setting-Up
- Install redux using the following command:
npm install --save redux
- Install react-redux using the following command:
npm install --save react-redux
- Install redux-thunk using the following command:
npm install --save redux-thunk
- Useful Resources:-
createStore()
- contained in
redux
package
- Redux has a method called
createStore()
which takes reducer
as an argument, and returns a reference to an object.
- use
createStore()
inside the index.js file or any global file.
reducer
is a method which takes 2 arguments : state and action and returns a new_state.
- state is the actual data that is located in the store and which would be replaced by the returned object. (keep in mind to not mutate the existing state, rather return new state.)
- action is passed on by the dispatch function which is called at the time of manipulation. It must contain type and may also contain payload.
- type is a string which mentions a reducer on what logic to execute.
- payload is an object/value which reducer may need while executing logic.
- Also, reducer must not have any async task.
createStore()
returns a reference to an object containing 5 functions:-
dispatch
: It's a function that passes an action object to the reducer which further updates the store.
- keep in mind, dispatching unknown type doesn't throw an error.
-
subscribe
: It contains a method which is executed whenever the store gets executed.
-
getState
: It returns the state in the store.
-
replaceReducer
:
-
[Symbol(observable)]
:
<Provider/> compoonent
- contained inside
react-redux
package
- as we know the best place to create new store is index.js because that's the place where we wrap the
<App/>
component with <Provider/>
component.
- This is done so because doing so, only will let all the component to be able to dispatch or subscribe to the store.
- For this, obviously there is a store prop in the
<Provider/>
component which takes the redux store.
- Use it like this:-
- Definition
-
const store = createStore(reducer)
- Implementation
-
<Provider store={store}/>
-
<App/>
-
</Provider>
-
Update the store immutably
refer here
few things to keep in mind
- while dealing with arrays and objects, don't do shallow cloning.
- try to keep the state as flat as possible
actionTypes
- Connecting reducer with dispatch using action.type can be very error-prone.
- To overcome this, create one more file (usually named actions.js) which contains all the action.type as constants.
- You can import it in the reducer as well as dispatch and use those constants to keep in sync between all of these different places
combineReducers()
- If you want to use multiple reducers, redux has a function called combineReducers().
- It takes an object as an argument, with key value pair with value being the imported reducers, and key is used in the subscribe method.
- Few things to keep in mind:-
- subscribe :- while defining mapStateToProps, you have to go further down to state.substate while accessing the state. Here substate will be the key defined inside combineReducers ie state.key.
- To pass values from one reducer to other, one needs to pass through action.payload only
When to use Redux
Types of State
- Local UI State - For eg., Show/Hide Backdrop
- Mostly handled within component
- Persistence State - For eg., All Users, All Posts, etc
- Stored on Server, relevent slice managed by Redux
- Client State - For eg., Is Authenticated?, Filter set by user, .etc...
Redux advanced
-
-
Redux file structure
- store: contains all folders needed for redux.
- actions
- actionTypes
- actionsCreators
- index
- reducers
- ActionCreator are just meant to create the actions before dispatching it to the reducers.
- ActionCreator can do all the async task jobs
- Reducers are the one where you put the actual logics which then returns the updated states to the store.
- Reducers can't do async task, it just runs sync codes.
- If you've async codes, run it inside actionCreators whereas, in case of sync codes, run it inside reducers.
-
-
-