简体   繁体   English

导入的React组件的状态和路由

[英]State and routing for imported React components

I have to write an integrated chat module that has two versions - a small in-site window (like facebook messenger) and full version that is opened in a new tab (a new react-router route). 我必须编写一个集成的聊天模块,该模块具有两个版本-一个小的站点内窗口(如facebook Messenger)和在一个新选项卡中打开的完整版本(一个新的react-router路由)。 So, this module exports two components: <ChatWindow /> and <ChatFullView /> for these views respectively. 因此,此模块分别导出两个组件: <ChatWindow /><ChatFullView />分别用于这些视图。

// core application
import {ChatWindow, ChatFullView} from 'chat-module';


// <PageContentWithChat /> contains imported <ChatWindow />
<Switch>
    <Route path='/' component={PageContentWithChat} />
    <Route path='/fullview' component={ChatFullView} />
</Switch>

So, the question is: 因此,问题是:
Where should I declare the redux store and manage it for both of them? 我应该在哪里声明redux存储并对其进行管理? (They must have one united store because the messages from the window version should be rendered in full view and vice versa) (它们必须具有一个统一的存储,因为窗口版本的消息应以全视图呈现,反之亦然)

EDIT: I wanted to control the module from the inside: 编辑:我想从内部控制模块:

// in module
const store = createStore(reducer);

....
<Provider store={store}>
    <ChatWindow />
    <ChatFullView />
</Provider>

But I'm afraid I won't be able to export these components separately as they are wrapped with <Provider /> . 但是恐怕我无法单独导出这些组件,因为它们被<Provider />包裹了。 How is it possible to solve this? 如何解决这个问题?

react-redux makes the store available through context via the Provider component. react-redux通过Provider组件通过上下文使存储可用。

Assuming <ChatWindow /> and <ChatFullView /> are connected components, you would wrap everything in <Provider /> , passing in your store as a prop. 假设<ChatWindow /><ChatFullView />是连接的组件,则将所有内容包装在<Provider /> ,作为道具传入您的商店。

Of course, you can organize all of this into different files, but this is the general idea. 当然,您可以将所有这些组织到不同的文件中,但这是一般的想法。

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { Switch, Route } from 'react-router';
import { BrowserRouter } from 'react-router-dom';
import { createStore } from 'redux';
import PageContentWithChat from 'path-to-page-content-with-chat';
import { ChatWindow, ChatFullView } from 'chat-module';

const store = createStore(/* reducers + initial message state passed in here... */);
const container = document.getElementById(/* id of your app container */);
const component = (
  <Provider store={store}>
    <BrowserRouter>
      <Switch>
        <Route path='/' component={PageContentWithChat} />
        <Route path='/fullview' component={ChatFullView} />
      </Switch>
    </BrowserRouter>
  </Provider>
);

render(component, container);

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

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