简体   繁体   English

我们如何覆盖或扩展 React.js 中的组件

[英]How do we can override or extend components in React.js

I need a piece of advice, we create an application for users that can be customized for a specific client.我需要一条建议,我们为用户创建了一个可以为特定客户定制的应用程序。 For example, we create our app for a customer A and let's imagine we have: a blue button with a text "Send" and a letter image near to the text例如,我们为客户 A 创建了我们的应用程序,假设我们有一个蓝色按钮,上面一个文本“发送”一个靠近文本的字母图像
Then a customer B says, that he needs this button like this: red button with a text "Email us" and an arrow image near to the text然后客户 B 说,他需要这样的按钮:带有文本“给我们发送电子邮件”和文本附近的箭头图像的红色按钮

This is just a super simple example, we can have much more changes and with huge components.这只是一个超级简单的例子,我们可以有更多的变化和巨大的组件。

We think this way, to create a first default app for customer A, all components and other stuff will be in common folders like we have only this app at all.我们这样认为,要为客户 A 创建第一个默认应用程序,所有组件和其他东西都将放在公共文件夹中,就像我们只有这个应用程序一样。 Then to create a folder for the customer B, there we will put only override components, for example, Button.js然后为客户 B 创建一个文件夹,我们将只放置覆盖组件,例如,Button.js

We need help to understand how should we to override or inherit properly a component (for our case a button component) from the default one?我们需要帮助来理解我们应该如何从默认组件正确地覆盖或继承一个组件(在我们的例子中是一个按钮组件)?

In React you can define a component with some properties.在 React 中,您可以定义具有某些属性的组件。 So, it's possible to create reusable components, not just split your code into modules.因此,可以创建可重用的组件,而不仅仅是将代码拆分为模块。 (You can even pack your components library an publish as NPM package for better dependency management). (您甚至可以将组件库打包为 NPM 包发布,以便更好地管理依赖项)。

When you use the component, you just provide those properties.当您使用组件时,您只需提供这些属性。 And the same component renders or behaves differently.并且相同的组件呈现或行为不同。

Parametrised component (by using props )参数化组件(通过使用props

export const Button = props => 
    <div>{props.text}</div>;
.button {
  // CSS styles here
}

.button--red {
  // CSS styles here
}

.button--icon-arrow {
  // CSS styles here
}

Then you use your component somewhere:然后你在某处使用你的组件:

<Button text="Email us" className="button button--red button--icon-arrow" />

Derived components (classes inheritance)派生组件(类继承)

First, you define the base component <Button /> .首先,您定义基础组件<Button />

Then you derive <AlertButton /> from <Button /> and override the functionality.然后从<Button />派生<AlertButton /> <Button />并覆盖功能。

export class Button extends React.Component {
    render() {
        return <button onClick={this.onClick}>{this.props.text}</button>;
    }

    onClick = e => {
        console.log(e);
    };
}

export class AlertButton extends Button {
    onClick = e => {
        alert(5);
    };
}

Then you can use both components separately in different ways.然后,您可以以不同的方式分别使用这两个组件。

<Button text="console.log()" />
<AlertButton text="alert()" />

Check out the materials:查看材料:

  1. React Docs / Components and Props React 文档/组件和道具
  2. CSS Tricks / BEM 101 CSS 技巧 / BEM 101

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM