redux-devtools-extension README

The project's README from github.com/zalmoxisus/redux-devtools-extension. As one markdown file: README.md.

⚠️⚠️⚠️🚨🚨🚨⚠️⚠️⚠️

This repo is no longer the home of the redux-devtools-extension. The new home is https://github.com/reduxjs/redux-devtools. Please file your issues and PRs there.

⚠️⚠️⚠️🚨🚨🚨⚠️⚠️⚠️

Redux DevTools Extension

Join the chat at https://gitter.im/zalmoxisus/redux-devtools-extension PRs Welcome OpenCollective OpenCollective

Demo

Installation

1. For Chrome

2. For Firefox

3. For Electron

4. For other browsers and non-browser environment

Usage

Note that starting from v2.7, window.devToolsExtension was renamed to window.__REDUX_DEVTOOLS_EXTENSION__ / window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__.

1. With Redux

1.1 Basic store

For a basic Redux store simply add:

 const store = createStore(
   reducer, /* preloadedState, */
+  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
 );

Note that preloadedState argument is optional in Redux's createStore.

For universal ("isomorphic") apps, prefix it with typeof window !== 'undefined' &&.

const composeEnhancers = (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;

For TypeScript use redux-devtools-extension npm package, which contains all the definitions, or just use (window as any) (see Recipes for an example).

const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

In case ESLint is configured to not allow using the underscore dangle, wrap it like so:

+ /* eslint-disable no-underscore-dangle */
  const store = createStore(
   reducer, /* preloadedState, */
   window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
  );
+ /* eslint-enable */

Note: Passing enhancer as last argument requires redux@>=3.1.0. For older versions apply it like here or here. Don't mix the old Redux API with the new one.

You don't need to npm install redux-devtools when using the extension (that's a different lib).

1.2 Advanced store setup

If you setup your store with middleware and enhancers, change:

  import { createStore, applyMiddleware, compose } from 'redux';

+ const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
+ const store = createStore(reducer, /* preloadedState, */ composeEnhancers(
- const store = createStore(reducer, /* preloadedState, */ compose(
    applyMiddleware(...middleware)
  ));

Note that when the extension is not installed, we’re using Redux compose here.

To specify extension’s options, use it like so:

const composeEnhancers =
  typeof window === 'object' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
      // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
    }) : compose;

const enhancer = composeEnhancers(
  applyMiddleware(...middleware),
  // other store enhancers if any
);
const store = createStore(reducer, enhancer);

See the post for more details.

1.3 Use redux-devtools-extension package from npm

To make things easier, there's an npm package to install:

npm install --save redux-devtools-extension

and to use like so:

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';

const store = createStore(reducer, composeWithDevTools(
  applyMiddleware(...middleware),
  // other store enhancers if any
));

To specify extension’s options:

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';

const composeEnhancers = composeWithDevTools({
  // Specify name here, actionsBlacklist, actionsCreators and other options if needed
});
const store = createStore(reducer, /* preloadedState, */ composeEnhancers(
  applyMiddleware(...middleware),
  // other store enhancers if any
));

There’re just few lines of code added to your bundle.

In case you don't include other enhancers and middlewares, just use devToolsEnhancer:

import { createStore } from 'redux';
import { devToolsEnhancer } from 'redux-devtools-extension';

const store = createStore(reducer, /* preloadedState, */ devToolsEnhancer(
  // Specify name here, actionsBlacklist, actionsCreators and other options if needed
));

1.4 Using in production

It's useful to include the extension in production as well. Usually you can use it for development.

If you want to restrict it there, use redux-devtools-extension/logOnlyInProduction:

import { createStore } from 'redux';
import { devToolsEnhancer } from 'redux-devtools-extension/logOnlyInProduction';

const store = createStore(reducer, /* preloadedState, */ devToolsEnhancer(
  // options like actionSanitizer, stateSanitizer
));

or with middlewares and enhancers:

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/logOnlyInProduction';

const composeEnhancers = composeWithDevTools({
  // options like actionSanitizer, stateSanitizer
});
const store = createStore(reducer, /* preloadedState, */ composeEnhancers(
  applyMiddleware(...middleware),
  // other store enhancers if any
));

You'll have to add 'process.env.NODE_ENV': JSON.stringify('production') in your Webpack config for the production bundle (to envify). If you use create-react-app, it already does it for you.

If you're already checking process.env.NODE_ENV when creating the store, include redux-devtools-extension/logOnly for production environment.

If you don’t want to allow the extension in production, just use redux-devtools-extension/developmentOnly.

See the article for more details.

1.5 For React Native, hybrid, desktop and server side Redux apps

For React Native we can use react-native-debugger, which already included the same API with Redux DevTools Extension.

For most platforms, include Remote Redux DevTools's store enhancer, and from the extension's context menu choose 'Open Remote DevTools' for remote monitoring.

2. Without Redux

See integrations and the blog post for more details on how to use the extension with any architecture.

Docs

Demo

Live demos to use the extension with:

Also see ./examples folder.

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]

<img src="https://opencollective.com/redux-devto

redux-devtools-extension (zalmoxisus/redux-devtools-extension)

Searching Database…

这是什么?一个面向 Redux 应用的浏览器扩展:把它指向你的 store 之后,开发时就能用 DevTools 查看.action 和状态的变化。
怎么安装这个扩展?可以从 Chrome 应用商店安装,Firefox 则从 Mozilla 附加组件商店安装。也可以自己构建:运行 npm i && npm run build:extension,然后把 ./build/extension 文件夹作为未打包扩展加载。
我的应用需要额外安装什么 npm 包吗?什么都不用装——只需要用扩展提供的 enhancer 包裹你的 store。不要 npm install redux-devtools(那是另一个库);TypeScript 项目请改用 redux-devtools-extension 这个包。使用 enhancer 需要 redux@>=3.1.0。
README 开头有个很大的警告——这个仓库还是扩展的所在地吗?已经不是了——这个仓库不再是扩展的所在地。它已经搬到了新的地方,issue 和 PR 都应该提交到那里。
它在 Chrome 之外能用吗,比如 Firefox 或 Electron 应用里?可以——Firefox 在 Mozilla 附加组件商店有专门的版本,Electron 应用可以通过 electron-devtools-installer 加载它。其他浏览器或非浏览器环境,README 指向了 remote-redux-devtools;React Native 应用可以用 react-native-debugger,它包含同样的 API。
它能在生产环境用,还是只能在开发时用?可以放进生产环境——README 说它在生产环境也有用。可以用 redux-devtools-extension/logOnlyInProduction 限制它,或者用 developmentOnly 禁用它;create-react-app 已经处理好了 NODE_ENV 的 envify,否则需要在你自己的 Webpack 配置里加上。
我的应用不是基于 Redux 的怎么办?它仍然可以接入——README 提供了与其他架构配合使用的集成方案链接,在线示例中还有一个 Angular ngrx store 的例子。
教程里说要用 window.devToolsExtension,但它是 undefined,为什么?这个全局变量在 v2.7 中改了名:请改用 window.__REDUX_DEVTOOLS_EXTENSION__ 或 window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__。
接入自己的应用之前,能先试用一下吗?可以——README 提供了在线示例的链接:Counter、TodoMVC、Redux Form、React Tetris 和一个 Angular ngrx 图书收藏示例,仓库里还有 ./examples 文件夹。
Creator
zalmoxisus
Size
11.3 MB
Stars
13.4K
Age
10y
Commits · 90 Days