Docsity

Front End Web development with ReactJs, Lecture notes of Web Design and Development

Partial preview of the text

Download Front End Web development with ReactJs and more Lecture notes Web Design and Development in PDF only on Docsity!

Advanced Client-

Side Development

Advanced React Concepts Component lifecycle, Forms and Controlled Components, React Router, Managing State with Context API

Recap:

  • So far we have learned about:
    • Rutime Environment outside the browser (Node.js and its package manager npm)
    • React Components (reusable interface components)
    • React Props
    • React States
    • Binding Events to update the state

Mounting Phase:

  • Occurs when a component is being created and inserted into the DOM.
  • Key methods:
    • constructor(): Initializing state and binding methods.
    • static getDerivedStateFromProps(): Used to update state based on props.
    • (^) render(): Returns the component JSX to be rendered.
    • componentDidMount(): Invoked after the component is mounted. Ideal for side effects like data fetching.

Mounting Phase 01: Constructor

  • The constructor() method is called before anything else, when the component is initiated, and it is the natural place to set up the initial state and other initial values.
  • The constructor() method is called with the props, as arguments, and you should always start by calling the super(props) before anything else, this will initiate the parent's constructor method and allows the component to inherit methods from its parent (React.Component).

Mounting Phase 03: render

  • The render() method is required, and is the method that actually outputs the HTML to the DOM.

Mounting Phase 04:

componentDidMount

  • The componentDidMount() method is called after the component is rendered.
  • This is where you run statements that requires that the component is already placed in the DOM. - We should use this to update the state, after something happened in the background for example.

Mounting Phase - Example

  • (^) You will need to establish react’s create app environment first to run this example. - npx create-react-app ourapp - Cd ourapp - npm start - Then download the app.js and component folder from blackboard and put them in src folder
  • See example: MountingExample

Updating Phase

  • The next phase in the lifecycle is when a component is updated.
  • A component is updated whenever there is a change in the component's state or props.
  • React has five built-in methods that gets called, in this order, when a component is updated: - (^) getDerivedStateFromProps() - shouldComponentUpdate() - render() - getSnapshotBeforeUpdate() - componentDidUpdate()
  • The render() method is required and will always be called, the others are optional and will be called if you define them.

Unmounting Phase

  • The next phase in the lifecycle is when a component is removed from the DOM, or unmounting as React likes to call it.
  • React has only one built-in method that gets called when a component is unmounted: - componentWillUnmount()

Unmounting Phase - Example

  • See Example: UnmountingExample

Uncontrolled Components

  • If you prefer not to use controlled components in React, you can use the traditional HTML form handling approach. In this case, you would rely more on refs to get the values of form elements when needed.
  • See Example: UncontrolledFormExample

Recommendation:

  • Controlled Components: This approach is generally recommended for most scenarios, especially when working with complex forms or when you want more predictability and control over the form's state. It aligns well with React's declarative approach to building UIs.
  • Uncontrolled Components: This approach might be suitable for simpler forms or when integrating with non-React code. It can be less verbose and more straightforward in certain situations.

Example

  • See Example: ReactRouter

Context API (Manage State)

  • The Context API in React provides a way to share values (like state or functions) across many components in a tree without having to pass the props manually at every level. It's particularly useful in scenarios where state needs to be accessed by multiple components that are not directly connected in the component tree.