哪种方式更好,使用`visible`属性还是在React组件上使用`open`方法的引用?

huangapple go评论46阅读模式
英文:

Which way is better for visible prop or open method on ref to react component?

问题

我正在尝试使用React 16封装一个组件。我已经参考了一些项目,现在有两种方法。哪一种更好?

在这种情况下,由于可见性状态在父组件中发生了变化,使用组件的开发者需要额外努力来解决性能问题,如果父组件有很多计算逻辑的话。

const [visible, setVisible] = useState<boolean>(false);

// 父组件
return <>
    <Button onClick={() => {
        setVisible(true);
    }}>打开模态框</Button>

    // 子组件
    <Modal visible={visible} />
</>;

或者使用 Ref:

// 子组件
const [visible, setVisible] = useState<boolean>(false);

React.useImperativeHandle(ref, () => ({
    open: () => { setVisible(true) },
    close: () => { setVisible(false) },
}))

return <div>
    {props.visible && children}
</div>;

// 父组件
const ModalRef = useRef<{ open: () => void; close: () => void } | null>(null);

return <>
    <Button onClick={() => {
        ModalRef.current.open();
    }}>打开模态框</Button>

    <Modal ref={ModalRef} />
</>;

在这种情况下,我不必操作DOM的引用,并且这种类型的组件不经常被销毁。引用可能会在浏览器中与父组件长时间保持。优点是开发者不再需要在父组件中管理其状态。

英文:

I'm trying to encapsulate a &lt;Drawer /&gt; and &lt;Modal /&gt; component using React 16. I have referred to some projects, and now I have two approaches. which one is better?

In this case, since the state of visibility is changed in the Parent, developers using the &lt;Modal /&gt; component would need to put in extra effort to address performance issues if the parent component has a lot of computational logic.

const [visible, setVisible] = useState&lt;boolean&gt;(false);

// Parent
return &lt;&gt;
    &lt;Button onClick={() =&gt; {
        setVisible(true);
    }}&gt;Open Modal&lt;/Button&gt;

    // Children
    &lt;Modal visible={visible} /&gt;
&lt;/&gt;;

Or use Ref:

// Children
const [visible, setVisible] = useState&lt;boolean&gt;(false);

React.useImperativeHandle(ref, () =&gt; {
    open: () =&gt; { setVisible(true) },
    close: () =&gt; { setVisible(false) },
})

return &lt;div&gt;
    {props.visible &amp;&amp; children}
&lt;/div&gt;;

// Parent
const ModalRef = useRef&lt;{ open: () =&gt; void; close: () =&gt; void } | null&gt;(null);

return &lt;&gt;
    &lt;Button onClick={() =&gt; {
        ModalRef.current.open();
    }}&gt;Open Modal&lt;/Button&gt;

    &lt;Modal ref={ModalRef} /&gt;
&lt;/&gt;;

In this case, I unnecessarily operated on a reference to the DOM, and this type of component is not frequently destroyed. The ref may persist with the parent in the browser for a long time. The advantage is that developers no longer need to manage its state in the parent component.

答案1

得分: 1

因此,在这种情况下,采用你的第二种方法。

尽管没有真正检查,但可能会假设存在性能瓶颈,而实际上并没有。这将变得过于复杂。

在完全普通的情况下,开发人员可能会在面临更棘手的问题时,试图追求早期性能特性。这就是为什么我建议首先追求高效率,然后再担心性能问题(仅是我的个人意见)。

在你的情况下,通常情况下我会使用第一种方法,但在有明确原因时会选择第二种方法。保持代码非常简单在过去几十年中已被证明是有用的。

组件解耦始终是React组件的主要争议点。这允许代码重用、模块化和代码维护。

如果在解耦过程中,你实现了父组件中的计算不再影响子组件的功能,那就是最好的方法。

就个人而言,我还没有遇到使用这种方法的重要缺点。

英文:

Well you said it. Heavy computation in the parent would affect the child so proper logic separation and independence is the best.

Therefore go with your method 2 in that case

Although without truly checking, one could be assuming a performance bottle neck where there is none. It then becomes an overkill.

In a completely common scenario, a developer could be aiming for early performance features while there is a trickier issue at hand. This is why I’d advise to aim to be productive first before worrying so much about performance (just my personal opinion)

In your case I will use the first approach under normal circumstances but will go for the second approach when there is clear reason to. Keeping code very simple has proven to be useful over the past decades now.

Component decoupling had always been the main bone of contention with react components. This allows code reuse, modularity and code maintenance.

If while decoupling, you achieved a feature where the computation in the parent no longer affects the child then that’s the best way to go.

Personally I haven’t come across important disadvantages with this approach.

huangapple
  • 本文由 发表于 2023年7月10日 10:10:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定