简体   繁体   English

React Redux附加组件

[英]React Redux append components

I have an project I'm working that is a HTML page builder of sorts. 我有一个项目,我正在工作,这是一个各种各样的HTML页面构建器。 The user can choose from a list of pre-designed HTML page sections (Header, sliders, content blocks, footers etc) from a menu and drop them onto a canvas to build a webpage. 用户可以从菜单中选择预先设计的HTML页面部分(页眉,滑块,内容块,页脚等)列表,然后将它们放到画布上以构建网页。 The end result is a downloadable zip file containing a static website. 最终结果是包含静态网站的可下载zip文件。

My application layout looks like this so far: 到目前为止,我的应用程序布局如下:

应用线框

I have part 1,2 & 3 covered. 我有第1,2和3部分。 Part 4, a slide out drawer, contains the HTML sections and when clicked I'd like to append the corresponding component to the main area of the page, this process would be repeated until the webpage has been built. 第4部分,一个滑出式抽屉,包含HTML部分,当单击时,我想将相应的组件附加到页面的主要区域,此过程将重复,直到构建网页。 Each component can then be edited and eventually saved as a page. 然后可以编辑每个组件并最终保存为页面。

What I am looking for advice on is how, using React/Redux, do I append full components to the main area onclick? 我正在寻找的建议是如何使用React / Redux将主要区域的完整组件附加到onclick上?

I assume I dispatch action/reducer referencing a component, but how do I actually add and keep track of the components appended to the main area? 我假设我派遣动作/减速器引用一个组件,但我如何实际添加和跟踪附加到主区域的组件? Using jQuery this would be a simple jQuery .load() but in React/Redux I have no idea, maybe a trick I can use using React Router? 使用jQuery这将是一个简单的jQuery .load()但在React / Redux中我不知道,也许我可以使用React Router使用的技巧?

Can anyone help point me in the right direction? 谁能帮助我指出正确的方向? Further reading etc. 进一步阅读等

Many thanks in advance. 提前谢谢了。

Loosely, you will want to map some type of ID to each component, then store those IDs within the store, something like mainArea = [id1, id6, id12] etc. Then your <MainArea /> component simply iterates over the props and references your established map to load the correct components. 松散地,您需要将某种类型的ID映射到每个组件,然后将这些ID存储在商店中,例如mainArea = [id1, id6, id12]等。然后您的<MainArea />组件简单地遍历道具和引用您建立的地图,以加载正确的组件。

Edit: Clarifying how to leverage a map of components. 编辑:阐明如何利用组件映射。

This will be roughly what you are looking for. 这将大致是您正在寻找的。 Let's assume the ids above are our components. 我们假设上面的id是我们的组件。 Somewhere in our codebase we would have a mapping of those ids to components: 在我们的代码库中的某个地方,我们将这些ID映射到组件:

const Component1 = React.createClass(...)
const Component6 = React.createClass(...)
const Component12 = React.createClass(...)

const componentMap = {
  id1: Component1,
  id6: Component6,
  id12: Component12,
}

Then, when your <MainArea /> component iterates through the ids that it has stored it would pull the corresponding component from the mapping above and render it. 然后,当您的<MainArea />组件遍历它存储的ID时,它将从上面的映射中提取相应的组件并进行渲染。

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

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