有没有办法使用电极confippet配置变量客户端?

时间:2016-11-30 01:13:23

标签: reactjs isomorphic-javascript walmart-electrode

使用电极组合库https://github.com/electrode-io/electrode-confippet - 有什么方法可以在反应组件中使用配置设置客户端?

我目前正在创建配置存储并从服务器索引文件传递它,但有更好的方法吗?

d: \\orders$

1 个答案:

答案 0 :(得分:1)

是的!我们目前正在这样做:

// 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);
};

这将在每次向Express发出请求时提供。

如果您需要在初始化期间设置应用所需的配置参数,还可以在express-server.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;
};

然后Electrode会像window.__PRELOADED__

那样使用它
相关问题