项目常用包
yarn add redux react-redux redux-thunkyarn add react-intl
react-intl
- 安装 国际化插件
yarn add react-intl 封装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 (
{this.props.children}
) } } const mapStateToProps = (state) => { return { locale: state.locale.locale, } } export default connect(mapStateToProps)(Locale)
3. 引入Locale组件(项目入口文件下)```jsximport React from 'react';import ReactDOM from 'react-dom';import { Provider } from 'react-redux';import store from './store';import App from './App';import LocaleProvider from './locale';ReactDOM.render(<React.StrictMode><Provider store={store}><LocaleProvider><App /></LocaleProvider></Provider></React.StrictMode>,document.getElementById('root'));
使用
- 函数式:需使用
injectIntl包裹组件 组件式:引入
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 (
) } } const mapStateToProps = (state) => { return { locale: state.locale.locale, } } const mapDispatchToProps = (dispatch) => { return { changlanguage: (value) => dispatch({<a href="#" onClick={this.change}><FormattedMessage id="exchange"></FormattedMessage></a><ul><li>{t.back}</li><li>{t.all}</li><li>{t.logout}</li><li>{t.unbind}</li></ul>
type: 'CHANGE_LANGUAGE',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
- chrome : 安装插件 Redux-DevTools
- 项目添加依赖:
yarn add redux-devtools-extension -D - 项目引入 ```jsx import {composeWithDevTools} from “redux-devtools-extension”
export default createStore(allReducers,composeWithDevTools(applyMiddleware(thunk)))
<a name="o0ymz"></a>## react-dev-inspector通过自定义,点击元素,跳转到对应文件1. Inspector 插件 `yarn add --dev react-dev-inspector `1. 添加 Inspector```jsximport React from 'react'import { Inspector } from 'react-dev-inspector'const InspectorWrapper = process.env.NODE_ENV === 'development'? Inspector: React.Fragmentexport const Layout = () => {// ...return (<InspectorWrapperkeys={['control', 'shift', 'command', 'c']} // default keys... // Props see below><Page /></InspectorWrapper>)}
标签中加入相关信息,如下所示
<div data-inspector-line="6"data-inspector-column="11"data-inspector-relative-path="src\examples\context-demo\Button.jsx">123</div>
重写 webpack插件
yarn add --dev react-app-rewired,使用react-app-rewired替换react-scripts"scripts": {"start": "react-app-rewired start","build": "react-app-rewired build","test": "react-app-rewired test","eject": "react-scripts eject"},
配置工具库
yarn add customize-cra --dev- 根目录下,新建
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(), ), )
> 注意:customize-cra依赖于react-app-rewired参考网址<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)<a name="RA4gD"></a>## babel-plugin-styled-componentsdebugger style-components> 未在 create-react-app 项目中运行备用方案:`react-app-rewire-styled-components`1. `yarn add --dev react-app-rewired`,使用`react-app-rewired `替换 `react-scripts`1. 安装 :`yarn add --dev react-app-rewire-styled-components`1. 配置 `config-overrides.js````javascriptconst rewireStyledComponents = require('react-app-rewire-styled-components');/* config-overrides.js */module.exports = function override (config, env) {return rewireStyledComponents(config, env, {fileName: true,displayName: true,})}
yarn add --dev react-app-rewired react-app-rewire-styled-components
参考网址
https://github.com/withspectrum/react-app-rewire-styled-components
customize-cra
yarn add uglifyjs-webpack-plugin customize-cra --dev
const { override, addBabelPlugin, addWebpackPlugin } = require('customize-cra')const UglifyJsPlugin = require('uglifyjs-webpack-plugin')/* config-overrides.js */module.exports = override(addWebpackPlugin(new UglifyJsPlugin({uglifyOptions: {compress: {drop_debugger: true,drop_console: true,},},}),),addBabelPlugin(['styled-components',{fileName: true,displayName: true,},]),)
