简体   繁体   English

如何在React中的组件内部进行操作?

[英]How to manipulate inside of a component in React?

We have a component <Book /> looks like following: 我们有一个<Book />组件,如下所示:

const Book = () => (<>
  <Title />
  <Thumbnail />
  <Author />
</>)

And I want to give it a little change into this component, such looks like: 我想对该组件进行一些更改,如下所示:

const CustomBook = () => (<>
  <Title />
  <h2>Hi, I am a subtitle</h2>
  <Thumbnail>
  <Author />
</>)

Let's say new version of <Book /> is released: 假设发布了<Book />新版本:

const Book = () => (<>
  <Title />
  <Author /> {/* Position is changed *}
  <Thumbnail /> {/* Position is changed *}
</>)

The position of <Author /> and <Thumbnail /> is now changed. <Author /><Thumbnail />的位置现在已更改。 But the <CustomBook /> doesn't follow the structure of the new <Book /> since I wrote my own structure. 但是<CustomBook />不遵循新的<Book />的结构,因为我编写了自己的结构。 But what I actually wanted to do in the <CustomBook /> component was just appending an element after the <Title /> component. 但是我实际上想在<CustomBook />组件中做的只是在<Title />组件之后追加一个元素。

Subsequently, one of my coworkers have suggested me the following approach: 随后,我的一位同事向我建议了以下方法:

<Book>
  {builder => builder
        // Assume that the keys are initially defined inside of the component
    .appendAfter('title', <h2>Foobar</h2>) 
    .appendBefore(...)
    .replace('foo', <Bar />)
    .setProps('foo', { bar: 100 })
  }
</Book>

But I don't think it's a good approach because: 1. The component is not self-descriptive. 但是我认为这不是一个好方法,因为:1.该组件不是自描述的。 2. The component is against React's principals, that we should not manipulate the DOM directly. 2.该组件违反了React的原则,我们不应该直接操作DOM。 (Although it's the virtual DOM) (尽管它是虚拟DOM)

I could define props such like afterTitle , but there are many components inside of the <Book /> component, therefore, it's going to be hard to define all of the props. 我可以定义诸如afterTitle类的道具,但是<Book />组件内部有许多组件,因此,很难定义所有道具。 And we also should be able to remove or replace the elements. 而且我们还应该能够删除或替换这些元素。

I'm curious if there is a react-ish way to achieve this goal. 我很好奇是否有一种反应式的方法可以实现这一目标。 Thank you in advance. 先感谢您。


(Edited) (编辑)的

TL;DR TL; DR

Is there a way to append/replace/remove some component inside of a component, by an internally existing component, no matter how component internally changes, without defining props. 有没有一种方法可以通过内部存在的组件在组件内部追加/替换/删除某个组件,而不管组件内部如何更改,而无需定义道具。

(Edited #2) (编辑#2)

I'll publish the <Book /> component on NPM. 我将在NPM上发布<Book />组件。 So developers can't modify it. 因此开发人员无法修改它。 I want to allow developers to customize the component's internal DOM tree with such API as insertAfter('title', <Subtitle />) . 我想允许开发人员使用诸如insertAfter('title', <Subtitle />)类的API自定义组件的内部DOM树。 But not by props because I want to make it flexible. 但不是靠道具,因为我想使其变得灵活。

If you checked the reconciliation 如果您检查reconciliation

1. Component instances are updated and reused based on their key. 1. 组件实例根据其键进行更新和重用。

2. React uses the key to match children in the original tree with children in the subsequent tree 2. React使用该键将原始树中的子代与后续树中的子代进行匹配

Please provide key to each component and siblings elements 请提供每个组件和同级元素的密钥

const CustomBook = () => (<>
  <Title  key={1}/>
  <h2 key={2}>Hi, I am a subtitle</h2>
  <Author key={3}/> 
  <Thumbnail key={4}/>
</>)

Note : for example purpose I have used 1,2,3.Please provide as per your requirements. 注意:例如,我使用了1,2,3。请根据您的要求提供。

If it is not necessary to have two separate components (one for <Book> and another <CustomBook> ), then you can simply pass props to Book , for example, subtitle . 如果没有必要使用两个单独的组件(一个用于<Book> ,另一个用于<CustomBook> ),则只需将props传递给Book ,例如subtitle Then in your Book component, you can check if it has a subtitle, and if so, display the subtitle. 然后,在您的Book组件中,可以检查它是否有字幕,如果有,请显示字幕。 Ie, 也就是说,

const Book = () => (<>
  <Title />
  { this.props.subtitle ? <h2>this.props.subtitle</h2> : null }
  <Thumbnail />
  <Author />
</>)

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

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