简体   繁体   English

我可以将 arguments 提供给 react-app 吗? (React 的依赖注入)

[英]Can I give arguments to a react-app? (dependency injection with React)

I would like to use a React-App, and give it some arguments (props), depending on where I embed it.我想使用一个 React-App,并给它一些 arguments(道具),这取决于我嵌入它的位置。

So one step back, we are using React actually as a library, and not the entire page is in React.所以退一步说,我们实际上将 React 用作一个库,而不是整个页面都在 React 中。 We have a very functioning website, and some parts are now being build in react.我们有一个功能非常强大的网站,现在正在构建一些部分。 Our motivation is: If you want the same component in another page, you can simply copy-paste 3 lines which include the css and js files, put a <div id="myReactAppRoot"></div> , and that's it.我们的动机是:如果你想在另一个页面中使用相同的组件,你可以简单地复制粘贴包括cssjs文件的 3 行,放一个<div id="myReactAppRoot"></div> ,就是这样。 Very quick, very clean, and instantly functioning.非常快速,非常干净,并且可以立即运行。

My question我的问题

When I have another "copy" of the React app, I would like to give it a different starting state.当我有 React 应用程序的另一个“副本”时,我想给它一个不同的起始 state。

  • One example use-case: I have two pages, in one I want the data to be grouped by X and in the other grouped by Y.一个示例用例:我有两个页面,在一个页面中,我希望数据按 X 分组,在另一个页面中按 Y 分组。
  • Another use-case, which I will have on my next project: disable editng, depending on the users permission level (the permission level is known by the main page).另一个用例,我将在我的下一个项目中使用:禁用编辑,具体取决于用户的权限级别(权限级别由主页知道)。

How can I achive this?我怎样才能做到这一点?

My current solution我目前的解决方案

My current solution is simply having a utils.js in the React app, which I use to access the windows object and get out the pieces I want:我当前的解决方案只是在 React 应用程序中添加一个utils.js ,我用它来访问windows object 并取出我想要的部分:

const utils = {
    fun1: window.myProject.forReact,fun1,
    groupElements: window.myProject.forReact.groupElements,
    permissionLevel: window.myProject.forReact.permissionLevel
};
export default utils;

Then importing utils in other components and using the functions/reading the values.然后在其他组件中导入utils并使用函数/读取值。 And of course making those objects available on the main page.当然,让这些对象在主页上可用。

But really, this feels wrong .但说真的,这感觉不对

My ideal solution would look a lot like the Vue.js way:我理想的解决方案看起来很像 Vue.js 方式:

<div id="app">
  {{ message }}
</div>

var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  }
})

Because I can just copy paste this bit, and change the Hello Vue!因为我可以只复制粘贴这一点,然后更改Hello Vue! , and all the sudden the starting state is different in my "second copy" of it. ,并且突然之间开始的 state 在我的“第二个副本”中有所不同。 Or even more cleverly, wrap it in a function that gets some initialState and put that JavaScript part in a file which I reference.或者更巧妙的是,将它包装在一个获得一些初始状态的initialState中,并将 JavaScript 部分放入我引用的文件中。

I have considered我考虑过

I have considered editing the compiled JavaScript code that is being referenced.我已经考虑编辑正在引用的编译 JavaScript 代码。 So far I only saw that the actual hooking into the myReactAppRoot -element is being done in the main.chunck.js , and editing that file seems too much like a hack (feels more wrong than my current solution).到目前为止,我只看到 myReactAppRoot 元素的实际挂钩是在myReactAppRootmain.chunck.js的,并且编辑该文件似乎太像 hack(感觉比我当前的解决方案更错误)。

To expand on Mike's comment, let's first translate your Vue app into React:为了扩展 Mike 的评论,让我们首先将您的 Vue 应用程序翻译成 React:

//html
<div id="root"></div>

//JS
const App = ({message}) => <div>{message}</div>
const rootElement = document.getElementById("root");
ReactDOM.render(
  <App message={"Hello, React!"}/>
  rootElement
);

A function that mounts an instance of App on a DOM node with id domId would be something like一个 function 将App实例安装在 DOM 节点上,id domId类似于

function mountAppWithMessageOnNode(message, domId){
  const rootElement = document.getElementById(domId);
  ReactDOM.render(
    <App message={message}/>
    rootElement
  );
}

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

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