[英]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.