Purpose目的
The Redux Toolkit package is intended to be the standard way to write Redux logic. It was originally created to help address three common concerns about Redux:
Redux Toolkit包替代了标准方式去写Redux逻辑。它最初是为了帮助解决有关Redux的三个常见问题而创建的:
- “Configuring a Redux store is too complicated”
- “I have to add a lot of packages to get Redux to do anything useful”
- “Redux requires too much boilerplate code”
- 配置Redux存储太复杂
- 必须添加很多**包才能做的事**
- Redux 需要太多样板代码
We can’t solve every use case, but in the spirit of create-react-app and apollo-boost, we can try to provide some tools that abstract over the setup process and handle the most common use cases, as well as include some useful utilities that will let the user simplify their application code.
我们无法解决所有用例,但是本着create-react-app和apollo-boost的精神,我试着提供一些可以抽象化设置过程并处理最常见用例的工具,以及包含一些有用的实用程序,这些实用程序将让用户简化他们的应用程序代码。
Because of that, this package is deliberately limited in scope. It does not address concepts like “reusable encapsulated Redux modules”, data caching, folder or file structures, managing entity relationships in the store, and so on.
因此,该软件包有意限制范围,它没有解决诸如“可重用的封装Redux模块”之类的概念,数据缓存,文件夹或者文件结构,管理存储中的实体关系等等。
That said, these tools should be beneficial to all Redux users. Whether you’re a brand new Redux user setting up your first project, or an experienced user who wants to simplify an existing application, Redux Toolkit can help you make your Redux code better.
也就是说,这些工具对所有Redux用户都是有益的,无论是设置第一个程序的新用户,或者经验丰富的想要简化已有应用程序的用户,Redux Toolkit可以帮助你改善你的Redux代码。
**
What’s Included包含什么
Redux Toolkit includes these APIs:
Redux Toolkit 包含这些API:
configureStore()
wraps createStore to provide simplified configuration options and good defaults. It can automatically combine your slice reducers, adds whatever Redux middleware you supply, includes redux-thunk by default, and enables use of the Redux DevTools Extension.
包装createStore以提供简单的配置项和良好的默认设置,它能自动合并你的reducers片,增加你供给的任意Redux中间件,默认包含redux-thunk,并允许使用Redux DevTools扩展。
createReducer()
that lets you supply a lookup table of action types to case reducer functions, rather than writing switch statements. In addition, it automatically uses the immer library to let you write simpler immutable updates with normal mutative code, like state.todos[3].completed = true.
这样你就可以为case reducer提供action types查询表,而不是编写switch语句。此外,它自动使用immer library让你用普通可变代码更简单编写不可变更新,例如state.todos[3].completed = true
.
createAction()
generates an action creator function for the given action type string. The function itself has toString() defined, so that it can be used in place of the type constant.