简体   繁体   English

有什么方法可以在客户端使用电极confippet配置变量?

[英]Is there any way to use the electrode confippet config variables client-side?

Using the electrode-confippet library, https://github.com/electrode-io/electrode-confippet - is there any way I can use the config settings client-side in the react component? 使用electrode-confippet库https://github.com/electrode-io/electrode-confippet-有什么方法可以在react组件中使用客户端的配置设置?

I am currently creating a config store and passing it from the server index file, but is there a better way? 我目前正在创建配置存储,并从服务器索引文件传递它,但是有更好的方法吗?

//index-view.js
import { config } from 'electrode-confippet';
const auth0Config = config.$('settings.auth0');

function createReduxStore(req, match) { // eslint-disable-line
  const initialState = {
    user: null,
    checkBox: { checked: false },
    number: { value: 999 },
    config: { auth: auth0Config },
  };

  const store = createStore(rootReducer, initialState);
  return Promise.resolve(store);
}

Yes! 是! We're doing it currently: 我们目前正在这样做:

// src/server/views/index-view.js
// …
module.exports = (req) => {
    const app = (req.server && req.server.app) || req.app;

    if (!app.routesEngine) {
        app.routesEngine = new ReduxRouterEngine({
            routes,
            createReduxStore,
            stringifyPreloadedState
        });
    }

    return app.routesEngine.render(req);
};

This will supply it on every request to Express. 这将在向Express发出的每个请求中提供。

If you need to set up config params that your app needs during initialisation, also create the store in express-server.js (and re-create it in index-view.js to avoid cross-connection contamination). 如果您需要设置应用程序在初始化期间需要的配置参数,请在express-server.js创建商店(并在index-view.js重新创建index-view.js以避免交叉连接污染)。

// express-server.js
// …
const loadConfigs = (userConfig) => {
    if (_.get(userConfig, 'plugins.electrodeStaticPaths.enable')) {
        // eslint-disable-next-line no-param-reassign
        userConfig.plugins.electrodeStaticPaths.enable = false;
    }

    return Confippet.util.merge(Confippet.config, userConfig);
};

// fetch appContext FIRST because components may need it to load, eg: language
const createReduxStore = (config) => store.dispatch({
        type: 'ADD_INITIAL_STATE',
        payload: { appContext: config.$('storeConfig') },
    })
    // eslint-disable-next-line no-console
    .catch(error => console.error('DISPATCH ERROR:', error))
    // return the now-hydrated store
    .then(() => store);

module.exports = function electrodeServer(userConfig, callback) {
    const promise = Promise.resolve(userConfig)
        .then(loadConfigs)
        .then(createReduxStore)
        // …

    return callback ? promise.nodeify(callback) : promise;
};

Then Electrode will make it available as something like window.__PRELOADED__ 然后Electrode将其作为window.__PRELOADED__东西提供window.__PRELOADED__

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

相关问题 Graphql 订阅未获取客户端变量 - Graphql subscription not getting client-side variables 如何在客户端访问环境变量(Node.js) - How to access environment variables client-side (Node.js) 为客户端搜索进行多参数过滤的优雅方式 - Elegant way to do multiple parameter filter for client-side search 在客户端应用程序中实现 Github OAuth 的正确方法? - Correct way to implement Github OAuth in client-side application? 有没有办法在 React 18 的客户端完成`renderToStaticMarkup` - Is there a way to accomplish `renderToStaticMarkup` on the client-side in React 18 有没有一种安全的方法可以在客户端存储登录凭据等数据? - Is there a secure way to store data like login credentials on client-side? NextJS 在自定义错误页面中使用客户端错误 - NextJS use client-side error in custom error page 在 ELK (Logstash) 中记录客户端/Javascript 的最佳方式 - Best Way to Log Client-Side/Javascript in ELK (Logstash) 在 cookies 中处理 JWT 令牌以在客户端进行身份验证的正确方法 - Correct way to handle JWT tokens in cookies for authentication on the client-side 如何在客户端 NextJS 中使用 Apollo GraphQL 订阅? - How to use Apollo GraphQL subscriptions in the client-side NextJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM