安装 Recoil

NPM

Recoil 软件包已经上传到 npm上了。欲下载最新稳定版本,请运行如下命令:

npm install recoil

或者使用 yarn

yarn add recoil

Bundler

Recoil installed via NPM pairs nicely with module bundlers such as Webpack or Rollup.

ES5 support

Recoil builds are not transpiled to ES5, and we do not support the use of Recoil with ES5. If you need to support browsers that do not provide ES6 features natively, you can do so by compiling your code with Babel and using preset @babel/preset-env. However, we do not support this and you may run into problems.

In particular, just like React, Recoil depends on the Map and Set types and other features of ES6. Emulation of these features using polyfills may result in far worse performance.

CDN

Since version 0.0.11, Recoil offers a UMD build that can be directly used in a <script> tag and exposes the symbol Recoil to the global namespace. We recommend linking to a specific version number and build to avoid unexpected breakage from newer versions:

<script src="https://cdn.jsdelivr.net/npm/recoil@0.0.11/umd/recoil.production.js"></script>

You can browse all Recoil files on the CDN at jsdelivr.

ESLint

If you are using eslint-plugin-react-hooks in your project. For example, with an eslint config like this:

// previous .eslint config
{
"plugins": [
"react-hooks"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
}

It is recommended to add 'useRecoilCallback' to the list of additionalHooks. With this change, ESLint will warn when the dependencies passed to useRecoilCallback() are specified incorrectly and suggests a fix. The format of additionalHooks is a regex string.

// modified .eslint config
{
"plugins": [
"react-hooks"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": [
"warn", {
"additionalHooks": "useRecoilCallback"
}
]
}
}