简体   繁体   English

订阅从另一个发布的一个 React 组件中的操作

[英]Subscribe to action in one React component published from another

Imagine the following situation: we've developed two smart components and released two npm packages.想象一下以下情况:我们开发了两个智能组件并发布了两个 npm 封装。 After that we imported components to third React application which developed by another team.之后,我们将组件导入另一个团队开发的第三个 React 应用程序。

package.json package.json

"dependencies": {
  "component-ui1": "0.1.1",
  "component-ui2": "0.1.0"
}

App.jsx应用程序.jsx

import React, { useEffect } from 'react';
import ComponentUI1 from 'component-ui1';
import ComponentUI2 from 'component-ui2';

export const App = () => {
    useEffect(() => {
       // Some asynchronous actions
    }, []);

    return (
        <>
            <ComponentUI1 someProp={true} />
            <ComponentUI2 someProp={false} />
        </>
    );
};

How can I generate an event in ComponentUI1 through DOM so that it triggers an action in ComponentUI2 , for example, updating some list?如何通过 DOM 在ComponentUI1中生成事件,以便触发ComponentUI2中的操作,例如更新某些列表? Preferably without modifying logic of source components.最好不修改源组件的逻辑。

The right way to do it is正确的做法是

  1. make component ComponentUI2 a fully controlled or partially controlled component.使组件 ComponentUI2 成为完全受控或部分受控的组件。 Let the patent pass the list in. Expose a prop someList让专利传入列表。暴露一个道具 someList
  2. Let component ComponentUI1 fire a event 'someEvent' when the said event is fired inside ComponentUI1当在 ComponentUI1 内触发所述事件时,让组件 ComponentUI1 触发事件“someEvent”
  3. Make the parent container make the update使父容器进行更新

    import React, { useEffect } from 'react'; import ComponentUI1 from 'component-ui1'; import ComponentUI2 from 'component-ui2'; export const App = () => { useEffect(() => { // Some asynchronous actions }, []); const [listAbc, setListAbc] = useState(0); const eventAbc = () = { //... update listAbc // setListAbc() } return ( <> <ComponentUI1 someProp={true} someEvent={eventAbc} /> <ComponentUI2 someProp={false} someList={listAbc} /> </> );

    }; };

import React, { useState, useEffect } from 'react';
import ComponentUI1 from 'component-ui1';
import ComponentUI2 from 'component-ui2';

export const App = () => {

    const [state, setState] = useState({ComponentUI1List: [], ComponentUI2List: []});

    useEffect(() => {
       // Some asynchronous actions
    }, []);

    return (
        <>
            <ComponentUI1 state={state} changeState={(data) => setState(data)} />
            <ComponentUI2 state={state} changeState={(data) => setState(data)} />
        </>
    );
};

now in your ComponentUI1 or 2 you can change ComponentUI1List or ComponentUI2List like so:现在在您的 ComponentUI1 或 2 中,您可以像这样更改ComponentUI1ListComponentUI2List

// ComponentUI1.jsx
import React from 'react';

export const App = (props) => 

     (
        <>
            <div className="container">
              <button onClick=(props.changeState({...props.state, ComponentUI2List: [{id: 1, title: "New Generated Data"}]}))
            </div>
        </>
    );

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

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