简体   繁体   English

是否可以从香草javascript中导入和使用react-redux组件

[英]Is it possible to import and use a react-redux component from vanilla javascript

I have two applications, one of them is written in vanilla javascript with html and css. 我有两个应用程序,其中之一是用javascript和html和css编写的。 The other one is a react + redux application. 另一个是react + redux应用程序。

Is it possible to import and use the components from my react + redux application inside my vanilla javascript application without introducing a dependency on react and redux? 是否可以从我的原始javascript应用程序内部的react + redux应用程序中导入和使用组件,而无需引入对react和redux的依赖关系?

Short answer, yes , depending how you've written the code. 简短的回答, 是的 ,这取决于您编写代码的方式。 Namely, assuming you are using ES6 Modules, or using a transpiler that will handle them as such. 即,假设您使用的是ES6模块,或使用将按原样处理它们的编译器。

Firstly, remember that react-redux is just an implementation of redux for react. 首先,请记住react-redux只是redux的实现。 Redux works fine by itself. Redux本身可以正常工作。

Assuming what you are talking about is reusing your reducers and action creators, then that will be fine - as all they are are pure functions. 假设您正在谈论的是重用化极器和动作创建者,那么这很好-因为它们都是纯函数。

For example say your app looks like: 例如,您的应用看起来像:

index.js index.js

import React from "react"; 
import store from "./store";
import {createUser} from "./actions"; 
import { Provider } from 'react-redux';

//etc. 

store.js store.js

import { createStore } from 'redux'
import rootReducer from '../reducers'

    export default function() {
        return createStore(rootReducer); 
    }

actions.js actions.js

   export  function addUser(name) {
       return {
            type: "ADD_USER", 
            payload: name, 
       }
   }

reducers.js reducers.js

   export default function (state ={}, action)  {

        return {
            value: action.payload
        }
    }

In this scenario, lets say you want to reuse the action, and the reducer. 在这种情况下,假设您要重用该操作和reducer。

Then you can just import the functions from these modules. 然后,您可以仅从这些模块中导入功能。 ESM modules are smart enough to just import what they need. ESM模块足够智能,仅需导入所需的内容即可。

On the otherhand, say you were importing the createStore (default) method, then you would also be importing the dependency on redux. 另一方面,假设您要导入createStore (默认)方法,那么您还将要导入对redux的依赖关系。

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

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