项目常用包

  1. yarn add redux react-redux redux-thunk
  2. yarn add react-intl

react-intl

  1. 安装 国际化插件 yarn add react-intl
  2. 封装Locale组件 ```jsx import React, { Component } from ‘react’ import { IntlProvider } from ‘react-intl’ import { connect } from ‘react-redux’ import enUS from ‘./en-US’ import zhCN from ‘./zh-CN’ const message = { ‘en-US’: enUS, ‘zh-CN’: zhCN, } class Locale extends Component { constructor(props) { super(props) }

    render() { const { locale } = this.props return (

    1. {this.props.children}

    ) } } const mapStateToProps = (state) => { return { locale: state.locale.locale, } } export default connect(mapStateToProps)(Locale)

  1. 3. 引入Locale组件(项目入口文件下)
  2. ```jsx
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { Provider } from 'react-redux';
  6. import store from './store';
  7. import App from './App';
  8. import LocaleProvider from './locale';
  9. ReactDOM.render(
  10. <React.StrictMode>
  11. <Provider store={store}>
  12. <LocaleProvider>
  13. <App />
  14. </LocaleProvider>
  15. </Provider>
  16. </React.StrictMode>,
  17. document.getElementById('root')
  18. );
  1. 使用

    1. 函数式:需使用 injectIntl包裹组件
    2. 组件式:引入 formattedMessage ```jsx import React, { Component } from ‘react’ import { FormattedMessage, injectIntl } from ‘react-intl’ import { connect } from ‘react-redux’ class Demo extends Component { constructor(props) { super(props)

      this.state = {} }

    change = () => { const { locale, changlanguage } = this.props changlanguage(locale === ‘zh-CN’ ? ‘en-US’ : ‘zh-CN’) } render() { const { intl } = this.props const t = intl.messages return (

    1. <a href="#" onClick={this.change}>
    2. <FormattedMessage id="exchange"></FormattedMessage>
    3. </a>
    4. <ul>
    5. <li>{t.back}</li>
    6. <li>{t.all}</li>
    7. <li>{t.logout}</li>
    8. <li>{t.unbind}</li>
    9. </ul>

    ) } } const mapStateToProps = (state) => { return { locale: state.locale.locale, } } const mapDispatchToProps = (dispatch) => { return { changlanguage: (value) => dispatch({

    1. type: 'CHANGE_LANGUAGE',
    2. value,

    }), } } export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(Demo)) ```

    此处使用 redux和react-redux,进行语言切换

官方网址
https://formatjs.io/
学习网址
https://zhuanlan.zhihu.com/p/39732502
https://juejin.cn/post/6977183729944887333
demo
https://github.com/Asplitline/t-react-demo/tree/react-intl

redux-tool

  1. chrome : 安装插件 Redux-DevTools
  2. 项目添加依赖:yarn add redux-devtools-extension -D
  3. 项目引入 ```jsx import {composeWithDevTools} from “redux-devtools-extension”

export default createStore(allReducers,composeWithDevTools(applyMiddleware(thunk)))

  1. <a name="o0ymz"></a>
  2. ## react-dev-inspector
  3. 通过自定义,点击元素,跳转到对应文件
  4. 1. Inspector 插件 `yarn add --dev react-dev-inspector `
  5. 1. 添加 Inspector
  6. ```jsx
  7. import React from 'react'
  8. import { Inspector } from 'react-dev-inspector'
  9. const InspectorWrapper = process.env.NODE_ENV === 'development'
  10. ? Inspector
  11. : React.Fragment
  12. export const Layout = () => {
  13. // ...
  14. return (
  15. <InspectorWrapper
  16. keys={['control', 'shift', 'command', 'c']} // default keys
  17. ... // Props see below
  18. >
  19. <Page />
  20. </InspectorWrapper>
  21. )
  22. }

标签中加入相关信息,如下所示

  1. <div data-inspector-line="6"
  2. data-inspector-column="11"
  3. data-inspector-relative-path="src\examples\context-demo\Button.jsx">
  4. 123
  5. </div>
  1. 重写 webpack插件 yarn add --dev react-app-rewired,使用react-app-rewired替换 react-scripts

    1. "scripts": {
    2. "start": "react-app-rewired start",
    3. "build": "react-app-rewired build",
    4. "test": "react-app-rewired test",
    5. "eject": "react-scripts eject"
    6. },
  2. 配置工具库 yarn add customize-cra --dev

  3. 根目录下,新建 config-overrides.js ```javascript const { ReactInspectorPlugin } = require(‘react-dev-inspector/plugins/webpack’) const { override, addBabelPlugin, addWebpackPlugin, } = require(‘customize-cra’)

module.exports = override( addBabelPlugin([ ‘react-dev-inspector/plugins/babel’, // plugin options docs see: // https://github.com/zthxxx/react-dev-inspector#inspector-babel-plugin-options { excludes: [ /xxxx-want-to-ignore/, ], }, ]), addWebpackPlugin( new ReactInspectorPlugin(), ), )

  1. > 注意:customize-cra依赖于react-app-rewired
  2. 参考网址<br />[https://github.com/zthxxx/react-dev-inspector](https://github.com/zthxxx/react-dev-inspector)<br />[https://github.com/timarney/react-app-rewired](https://github.com/timarney/react-app-rewired)<br />[https://github.com/arackaf/customize-cra](https://github.com/arackaf/customize-cra)<br />学习网址<br />[https://juejin.cn/post/6901466406823575560](https://juejin.cn/post/6901466406823575560)
  3. <a name="RA4gD"></a>
  4. ## babel-plugin-styled-components
  5. debugger style-components
  6. > 未在 create-react-app 项目中运行
  7. 备用方案:`react-app-rewire-styled-components`
  8. 1. `yarn add --dev react-app-rewired`,使用`react-app-rewired `替换 `react-scripts`
  9. 1. 安装 :`yarn add --dev react-app-rewire-styled-components`
  10. 1. 配置 `config-overrides.js`
  11. ```javascript
  12. const rewireStyledComponents = require('react-app-rewire-styled-components');
  13. /* config-overrides.js */
  14. module.exports = function override (config, env) {
  15. return rewireStyledComponents(config, env, {
  16. fileName: true,
  17. displayName: true,
  18. })
  19. }
  1. yarn add --dev react-app-rewired react-app-rewire-styled-components

参考网址
https://github.com/withspectrum/react-app-rewire-styled-components

customize-cra

  1. yarn add uglifyjs-webpack-plugin customize-cra --dev
  1. const { override, addBabelPlugin, addWebpackPlugin } = require('customize-cra')
  2. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  3. /* config-overrides.js */
  4. module.exports = override(
  5. addWebpackPlugin(
  6. new UglifyJsPlugin({
  7. uglifyOptions: {
  8. compress: {
  9. drop_debugger: true,
  10. drop_console: true,
  11. },
  12. },
  13. }),
  14. ),
  15. addBabelPlugin([
  16. 'styled-components',
  17. {
  18. fileName: true,
  19. displayName: true,
  20. },
  21. ]),
  22. )