React: 组件介绍 Components

这篇具有很好参考价值的文章主要介绍了React: 组件介绍 Components。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Component is one of the core building blocks of React. In other words, we can say that every application you will develop in React will be made up of pieces called components. Components make the task of building UIs much easier.

“Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.”

React: 组件介绍 Components,react.js,javascript,前端 

Here we can see a UI broken down into multiple individual pieces called components and work on them independently and merge them all in a parent component which will be your final UI.

Now let's study the different types of components that we have in React:

Dumb / Stateless / Presentational Component

🦄 Dumb components focus on how things look.

  • Dumb components are also called ‘presentational’ components because their only responsibility is to present something to the DOM. Once that is done, the component is done with it.
  • There will be no logic at all inside this component therefore it is called a dumb component. Because dumb components only focus on the presentation (UI Component), it is ideally the most reusable component.
  • This component is often just Javascript functions and only has a render() method. It also does not have any state or lifecycle hooks. However, it still can receive some data and function from the parents via props.

Common characteristics of dumb components:

  1. Focus on the UI: Almost all basic UI components should be considered dumb components. Examples include loaders, modals, buttons, inputs, etc.
  2. Accept props: Dumb components accept props to allow them to be dynamic and reusable. For example, you might send the title of a button in props from the parent component to allow it to have a unique name.
  • Require no app dependencies: Other than UI packages, like Reactstrap, dumb components do not require dependencies.
  • Rarely include state: The only instance where a dumb component has state is for manipulating the UI itself, not application data. Some examples of where a dumb component might have state would be button groups, tabs, switches and other UI elements that do not impact the data, only the UI.

Let’s take a look at our sign-in form example. Almost every component can be a reusable dumb component including the container, header, inputs and button.

React: 组件介绍 Components,react.js,javascript,前端

Highlighted Dumb Component

Smart / Stateful / Container Component

🤓 Smart components focus on how things work.

  • Smart components (or container components) on the other hand have a different responsibility. Because they have the burden of being smart, they are the ones that keep track of the state and care about how the app works. Using the container design pattern, the container components are separated from the presentational components and each handles their own side of things. The container components do the heavy lifting and pass the data down to the presentational components as props.
  • Container component pattern is class-based components and have constructor() functions. We usually initialize the state inside the constructor, although you can remove the constructor and still have a state.

Common characteristics of smart components include:

  1. Manipulates Data: Smart components can fetch, capture changes and pass down application data.
  2. Call Redux, Lifecycle methods, APIs, Libraries, etc: These components are called smart for a reason! They are responsible for calling libraries and functionality.
  3. Manage state: Smart components are responsible for managing state and knowing when to re-render a component.
  4. Rarely includes styling: Since dumb components focus on styling, it allows the smart component to focus on functionality without the clutter of styles too.

Pure Component

  • Pure Component is one of the most significant ways to optimize React applications.
  • The usage of Pure Component gives a considerable increase in performance because it reduces the number of render operations in the application.
  • So above there is an example of a very simple Welcome Pure Component. When you use this in your Parent Component, you will see Welcome Component will not re-render whenever the Parent Component will re-render
  • This is because PureComponent changes the life-cycle method shouldComponentUpdate and adds some logic to automatically check whether a re-render is required for the component. This allows a PureComponent to call the method render only if it detects changes in state or props.

Higher-Order Component

  • A higher-order component in React is a pattern used to share common functionality between components without repeating code.A higher-order component is actually not a component though, it is a function. A HOC function takes a component as an argument and returns a component. It transforms a component into another component and adds additional data or functionality. In short:
const NewComponent = (BaseComponent) => {
  // ... create new component from old one and update
  return UpdatedComponent
}
  • Two HOC’s implementations that you may be familiar with in the React ecosystem are connect from Redux and withRouter from React Router. The connect function from Redux is used to give components access to the global state in the Redux store, and it passes these values to the component as props. The withRouter function injects the router information and functionality into the component, enabling the developer to access or change the route.
  • A higher-order component is a function that takes a component as an argument and returns a component. This means that a HOC will always have a form similar to the following:
  • The higherOrderComponent is a function that takes a component called WrappedComponent as an argument. We create a new component called HOC which returns the <WrappedComponent/> from its render function. While this actually adds no functionality in the trivial example, it depicts the common pattern that every HOC function will follow. We can invoke the HOC as follows:
const SimpleHOC = higherOrderComponent(MyComponent);

Let us take a look at a simple example to easily understand how this concept works. The MyHOC is a higher-order function that is used only to pass data to MyComponent. This function takes MyComponent, enhances it with newData and returns the enhanced component that will be rendered on the screen.

If we run the app, we will see that data is passed to MyComponent.

React: 组件介绍 Components,react.js,javascript,前端

Higher-order component considerations

  • A HOC should be a pure function with no side-effects. It should not make any modifications and just compose the original component by wrapping it in another component.
  • Do not use HOC’s in the render method of a component. Access the HOC outside the component definition.
  • Static methods must be copied over to still have access to them. A simple way to do this is the hoist-non-react-statics package.
  • Refs are not passed through.

Controlled Component

  • A controlled component is bound to a value, and its changes will be handled in code by using event-based callbacks. Here, the input form element is handled by the react itself rather than the DOM. In this, the mutable state is kept in the state property and will be updated only with setState() method. The state within the component serves as “the single source of truth” for the input elements that are rendered by the component.
  • Controlled components have functions that govern the data passing into them on every onChange event occurs. This data is then saved to state and updated with setState() method. It makes component have better control over the form elements and data.

 https://javascript.plainenglish.io/react-all-about-components-35650a02ff50文章来源地址https://www.toymoban.com/news/detail-687460.html

到了这里,关于React: 组件介绍 Components的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • React类组件和函数组件对比-Hooks的介绍及初体验

    Hooks出现的原因 Hook 是 React 16.8 的新增特性,它可以让我们在不编写class的情况下, 使用state以及其他的React特性( 比如生命周期 ) 。 我们先来思考一下class组件相对于函数式组件有什么优势?比较常见的是下面这些优势 : class组件可以定义自己的state,用来保存组件自己内部的状

    2024年01月16日
    浏览(40)
  • React styled-components(二)—— props、attrs属性

    styled-components 可以 props 穿透,把属性穿透到元素中。 通常,用 css 的 input 组件实现一个密码输入框写法如下: 接下来用 styled-components 来实现,首先生成一个 input 组件,新建 Demo.js 文件: App.js 中引入 Demo.js 文件: 页面效果: 接下来,给生成的 ContextP 组件添加 type=password

    2023年04月18日
    浏览(40)
  • Node.js npm V8 React Express的运行配合关系:构建JavaScript应用的基石

    目录 Node.js 和 V8 引擎 Node.js 和 npm LTS(Long Term Support) React Node.js的作用 Express Node.js 和 V8 引擎 Node.js 使用 Google 的 V8 JavaScript 引擎 来执行 JavaScript 代码。V8 是一个高性能的 JavaScript 和 WebAssembly 引擎,用于在 Google Chrome 浏览器和 Node.js 中运行 JavaScript。 V8 引擎的更新 通常包括

    2024年03月12日
    浏览(64)
  • Testing Angular, VueJS, and React Components with Jest

    作者:禅与计算机程序设计艺术 在过去的几年里,React、Angular和Vue等前端框架都获得了越来越多开发者的青睐,并且取得了不俗的成绩。这些前端框架的出现给前端开发领域带来了许多新鲜的机会。特别是在面对复杂业务需求时,测试驱动开发(TDD)方法对于保证项目质量至

    2024年02月06日
    浏览(41)
  • React-Hoc高阶组件与css-in-js技术

    目录 一、什么是React-Hoc 二、什么是高阶组件 三、什么是css-in-js技术 React-HOC(Higher-Order Component,高阶组件)是React中一种用于重用组件逻辑的模式。它本质上是一个函数,接受一个组件作为参数并返回一个新的组件。 HOC可以用于在不修改原始组件的情况下,为组件添加额外

    2024年01月24日
    浏览(45)
  • React学习笔记(番外一)——video.js视频播放组件的入门及排坑经历

    很久没有静下心写博客了。近段时间接到一个任务,前端页面要加上视频播放功能。实现加排坑前后花了三天时间(别问我问什么这么久😂),觉得还是有必要记录一下的。 这一部分有必要写在最前面,避免你看了一长串安装、引入、代码,然后发现自己想要播放的视频格

    2024年02月02日
    浏览(47)
  • Mobx在非react组件中修改数据,在ts/js中修改数据实现响应式更新

    我们都之前在封装mobx作为数据存储的时候,使用到了useContext作为包裹,将store变成了一个hooks使用,封装代码: 但是我们都知道hooks只能在函数组件中或者hooks中使用,不能在ts/js代码中使用,但是我这里有一个需求,想每次发送接口请求的时候,做一个后置处理器,用于获取

    2024年02月11日
    浏览(48)
  • JavaScript之React

    前言 React 是一个用于构建用户界面的 JavaScript 库,由 Facebook 开发。它可以让开发者编写可重复使用的 UI 组件,并且可以自动地更新 UI。React 的运行原理可以分为两个部分:虚拟 DOM 和组件更新。 虚拟 DOM React 使用虚拟 DOM 来代表实际的 DOM 树。虚拟 DOM 是一个轻量级的 JavaSc

    2024年02月06日
    浏览(42)
  • 【React学习】React组件生命周期

    在 React 中,组件的生命周期是指组件从被创建到被销毁的整个过程。React框架提供了一系列生命周期方法,在不同的生命周期方法中,开发人员可以执行不同的操作,例如初始化状态、数据加载、渲染、更新等。一个组件的生命周期大致可以分为三个阶段,即组件挂载时,更

    2024年02月12日
    浏览(40)
  • 如何在React中构建动态下拉组件 - 解释React复合组件模式

    下拉菜单长期以来一直是网站和应用程序中的重要组成部分。它们是用户交互的默默英雄,通过简单的点击或轻触默默地促进着无数的操作和决策。 今天你可能已经遇到了其中之一,无论是在你最喜爱的在线商店上选择类别,还是在注册表单上选择你的出生日期。 但如果我

    2024年04月26日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包