简体   繁体   English

Unstated.js访问其他容器中的容器

[英]Unstated.js accessing container within other container

I've just started looking into unstated and I've bumped into an issue for which I can't find an answer yet. 我刚刚开始研究未声明的内容,遇到了一个尚无法找到答案的问题。

Suppose I have two containers: 假设我有两个容器:

  • ContainerA 集装箱A
  • ContainerB 集装箱B

What should I do if I wanted to either access ContainerB state in ContainerA or even call a method of ContainerB in ContainerA? 如果我想访问ContainerA中的ContainerB状态或什至在ContainerA中调用ContainerB的方法,该怎么办?

The only way I see so far is to call ContainerA method and pass ContainerB instance manually as a separate argument which seems extremely bad and repetitive given I may need to do the same in multiple places... 到目前为止,我看到的唯一方法是调用ContainerA方法并手动将ContainerB实例作为一个单独的参数传递,这似乎非常糟糕且重复,因为我可能需要在多个位置执行相同的操作...

Have a look at the unstated docs around dependency injection. 看看有关依赖注入的未声明文档。 That allows you to instantiate your containers before adding them to your provider. 这样,您就可以实例化容器,然后再将其添加到提供程序中。 So you can wire together your containers any way you like. 因此,您可以按自己喜欢的任何方式将容器连接在一起。 Seems legit? 似乎是合法的?

const containerA = new ContainerA();
const containerB = new ContainerB({ containerA });

render(
  <Provider inject={[containerA, containerB]}>
    <App />
  </Provider>
);

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

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