简体   繁体   English

我怎样才能在 Redux 商店内保留不会更改的 state?

[英]How can I keep state that will not change inside Redux store?

I'm building a Single App that will do SSR (server side rendering) and I'm using React + Redux .我正在构建一个将执行 SSR(服务器端渲染)的单一应用程序,并且我正在使用React + Redux

I've just started to implement Redux in this app.我刚刚开始在此应用中实现Redux It was previously built app using only React's useState , useContext etc.它以前是仅使用 React 的useStateuseContext等构建的应用程序。

The fact is that sometimes I need my app code to be aware of the environment that it's running, either ON_CLIENT or ON_SERVER , to skip some window.something statement, for example.事实上,有时我需要我的应用程序代码知道它正在运行的环境,例如ON_CLIENTON_SERVER以跳过一些window.something语句。

Before Redux , I was doing the following:Redux之前,我正在做以下事情:

index.js (this could be the index.js of my client bundle or my server bundle) index.js (这可能是我的客户端包或服务器包的 index.js)

ReactDOM.render(
  <App
    ON_SERVER={false}               // THIS IS TRUE ON SERVER CODE
    ON_CLIENT={true}                // THIS IS TRUE ON CLIENT CODE
    ... other stuff
  />
,document.getElementById('root')
);

App.js应用程序.js

...
const environment = {
  ON_SERVER: props.ON_SERVER,
  ON_CLIENT: props.ON_CLIENT
}

...

// PROVIDING IT USING REACT CONTEXT

return (
  <EnvironmentContext.Provider value={environment}>
    <MyComponents/>
  </EnvironmentContext.Provider>
);

And then, inside some component, I can do this pattern:然后,在某个组件内部,我可以执行以下模式:

SomeComponent.js SomeComponent.js

const {ON_CLIENT} = useContext(EnvironmentContext);
ON_CLIENT && window.something;

And I want to improve this pattern with Redux .我想用Redux改进这种模式。

I want to keep this in the Redux store, so I can get rid of the EnvironmentContext and access it with:我想将它保留在 Redux 商店中,这样我就可以摆脱EnvironmentContext并通过以下方式访问它:

const {ON_CLIENT} = useSelector((state) => state.environment);

So I've thought of doing:所以我想过这样做:

index.js索引.js

const store = createStore(rootReducer, {
  environment: {
    ON_CLIENT: true,               // THIS IS TRUE ON CLIENT CODE
    ON_SERVER: false               // THIS IS TRUE ON SERVER CODE
  }
});

But since I don't have a corresponding reducer for this piece of state ( environment ), I got this error msg:但是由于我没有针对这块 state( environment )的相应减速器,我收到了这个错误信息:

redux.js:319 Unexpected key "environment" found in preloadedState argument passed to createStore. redux.js:319在传递给 createStore 的 preloadedState 参数中发现意外键“environment”。 Expected to find one of the known reducer keys instead: "auth", "appVersion", "siteData".期望找到一个已知的缩减器密钥:“auth”、“appVersion”、“siteData”。 Unexpected keys will be ignored.意外的键将被忽略。

NOTE: auth , appVersion and siteData are pieces of state which I have corresponding reducers for.注意: authappVersionsiteData是 state 的一部分,我有相应的减速器。

Here is my rootReducer :这是我的rootReducer

const rootReducer = combineReducers({
  auth: updateAuth,
  appVersion: updateClientVersion,
  siteData: updateSiteData
});

QUESTION问题

Can I have some piece of state that will not change, and therefore is not handled by any reducer?我可以有一些不会改变的 state ,因此不被任何减速器处理吗? Or in this case I do need to set up some dummy reducer just to always return that same state?或者在这种情况下,我确实需要设置一些虚拟减速器只是为了始终返回相同的 state? PS: It does the trick, but it feels wrong, though. PS:它确实有效,但感觉不对。

// NOTE: I will always preload this state, in the `createStore` call, so the state will never be undefined.

function returnEnvironment(state={}, action) {
  return state;
}

const rootReducer = combineReducers({
  auth: updateAuth,
  appVersion: updateClientVersion,
  siteData: updateSiteData,
  environment: returnEnvironment
});

Does anybody have a better alternative to this?有人对此有更好的选择吗?

I've looked at this discussion: https://github.com/reduxjs/redux/issues/1457我看过这个讨论: https://github.com/reduxjs/redux/issues/1457

There are some suggestions to populate the global object, but I'd rather keep it all inside React and Redux .有一些填充全局 object 的建议,但我宁愿将其全部保留在ReactRedux中。

PS: Sorry for the long question, but I wanted to make my use case as clear as I could, so somebody might have a better pattern. PS:很抱歉问了这么长的问题,但我想让我的用例尽可能清楚,所以有人可能会有更好的模式。

Redux is designed for data centralisation and management such as loading some dynamic data changing it during the application lifetime and so on.Since you are not going to change that values because your application can't run in both environments on a same time or switch between them that mean you don't need to change them during application lifetime and if some variable should not change value it should be declared as a CONSTANT. Redux 专为数据集中和管理而设计,例如加载一些动态数据,在应用程序生命周期内更改它等等。因为您不会更改这些值,因为您的应用程序不能同时在两个环境中运行或在两者之间切换它们意味着您不需要在应用程序生命周期内更改它们,如果某些变量不应更改值,则应将其声明为 CONSTANT。 So declare it as a constant and important whenever you need to access it.因此,只要您需要访问它,就将其声明为常量和重要值。

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

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