React JS
React JS
Introduction
- Developed and maintained by Facebook and Instagram.
- A JavaScript library for creating user interfaces.
- Serves as the view of MVC architecture.
- Suitable for large web application which use data and change over the time without reloading the entire page.
- React Native caters developing mobile application for various platforms and React VR caters developing VR applications.
- Aims at Speed, Simplicity and Scalability.
● Notable features
- One-Way data flow.
- Single source of truth - Data is originated and kept in one place, data is immutable.
- Data flow from the parent component to the child component.
- Action flow from child component to parent component.
- Single source of truth - Data is originated and kept in one place, data is immutable.
- Virtual DOM
- DOM manipulation is cost instead react create a virtual DOM tree and compare it with the rendered tree and update only what is changed.
- JSX
- React JS language for defining user interfaces.
- HTML like syntax.
- Prevents cross-site scripting attacks by converting expressions to strings.
- React JS language for defining user interfaces.
● Props and State
- Props are used to pass data and behaviours from container components to child components.
- Props are read only.
- State is used to keep data which can be updated.
- State is read write enabled.
- Use setState({}) method to update state.
- setState method is asynchronous so, if you want to to use state and props inside setState method use the second form of the method setState((state,props)=>{}).
● Component life cycle
- React components extended from React.Component parent class inherit a set of methods that belong to react lifecycle.
- Render and commit state.
- Controlled and uncontrolled.
- Side effects.
- Render and commit state.
- constructor
- JS constructor.
- Declare state and bind context to life cycle events.
- Do not call setState.
- Always call super(props) to bind this.props.
- JS constructor.
- render
- Rendering of the component.
- Called everytime state or props are getting changed.
- Can return JSX, Portal, Fragments, Strings, boolean etc.
- Rendering of the component.
- componentDidMount
- Fires after component is mount to DOM.
- Can interact with DOM.
- Ajax calls and update the state.
- Fires after component is mount to DOM.
- getDerivedStateFromProps
- Use this if you want to update your state from props.
- React highly recommends not to use this.
- Highly discourage conditionally setting state from props.
- Use this if you want to update your state from props.
- shouldComponentUpdate
- To decide whether to update the component or not.
- Only as a performance enhancement, if required.
- To decide whether to update the component or not.
- componentDidUpdate
- After component got updated.
- Will not run if shouldComponentUpdate return false.
- Use this to interact with DOM after a Component update.
- After component got updated.
- componentWillUnmount
- Before removing the Component.
- Should not call setState.
- Clean up tasks.
- Before removing the Component.
- componentDidCatch
- After an error from decedent component.
- getDerivedStateFromError to get error related State.
- componentDidCatch for logging and side effects.
- After an error from decedent component.
● Babel
- Most popular JavaScript transpiler.
- Convert JavaScript es6 features and beyond to latest compatible version.
- Babel polyfills are available to support new global objects. It uses core-js polyfills.
- So far Babel compatibility with new features has been one of the best.
- Debugging enabled via source maps.
- In built code optimization.
- Supports JSX (XML like syntax extension) and FLOW (Static type checker).
● Webpack
- Most famous module bundler.
- Support for build system as well as module bundling.
- Split code into multiple files.
- Supports CommonJS and AMD.
- Extend using loaders (transpiling, css transformation, image optimization).
- Supports production optimizations (minification, uglification).
- Easy configuration with 4 steps - entry -> loaders -> plugins -> output.
● Parcel
- Fastest module bundler.
- Relatively new to community.
- Conventions over configurations.
- Support JSX, SCSS, LESS, SVG, TypeScript etc.
- Plugins available for extension and plugins are automatically detected.
Comments
Post a Comment