react-redux

Getting Started with React Redux | React Redux

1.安装

# If you use npm:
npm install react-redux

# Or if you use Yarn:
yarn add react-redux

cnpm i @types/react-redux -D

2.引用 index.tsx

import { Provider } from "react-redux";
import store from "./redux/store";

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById("root")
);

3. connect

// HOC
import { connect } from "react-redux";
import { RootState } from "../../redux/store";
import { Dispatch } from "redux";



const mapStateToProps = (state: RootState) => {
  return {
    language: state.language,
    languageList: state.languageList
  }
}

const mapDispatchToProps = (dispatch: Dispatch) => {
  return {
    changeLanguage: (code: "zh" | "en") => {
      const action = changeLanguageActionCreator(code);
      dispatch(action);
    },
    addLanguage: (name: string, code: string) => {
      const action = addLanguageActionCreator(name, code);
      dispatch(action);
    },
  };
};

type PropsType = RouteComponentProps & // react-router 路由props类型
  WithTranslation & // i18n props类型
  ReturnType<typeof mapStateToProps> & // redux store 映射类型 
  ReturnType<typeof mapDispatchToProps>; // redux dispatch 映射类型 

class HeaderComponnet extends React.Component<PropsType> {
    this.props.XXXXX
}

export const Header = connect(mapStateToProps, mapDispatchToProps)(
  withTranslation()(withRouter(HeaderComponnet))
);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React-Redux是一个结合了React和Redux的库,它使得在React应用中集成状态管理变得更加容易。Redux是一个单一来源的状态管理库,而React-Redux提供了一组工具来帮助你将Redux的状态管理与React组件结合起来。 安装React-Redux的步骤通常是这样的: 1. 首先,确保你已经安装了Node.js和npm(Node包管理器)。 2. 在你的项目目录中,打开终端或命令提示符。 3. 使用npm或yarn来安装`react-redux`和`redux`库。如果你还没有安装`redux`,可以先安装: ```bash npm install redux ``` 或者使用yarn: ```bash yarn add redux ``` 4. 安装`react-redux`: ```bash npm install react-redux ``` 或者: ```bash yarn add react-redux ``` 5. 引入到你的项目中。通常会创建一个store来保存全局状态,以及将store连接到React组件上: ```javascript // 在index.js或App.js中 import { createStore } from 'redux'; import rootReducer from './reducers'; // 根 reducer,合并所有模块的reducer const store = createStore(rootReducer); // 如果你在使用react-redux,还需要引入Provider组件: import { Provider } from 'react-redux'; import App from './App'; // 你的React应用组件 function Root() { return ( <Provider store={store}> <App /> </Provider> ); } ReactDOM.render(<Root />, document.getElementById('root')); ``` 6. 在React组件中,使用`connect`函数从store中提取数据并处理dispatch动作: ```javascript import { useSelector, useDispatch } from 'react-redux'; function MyComponent() { const myState = useSelector(state => state.mySlice); // 选择store中的状态 const dispatch = useDispatch(); // 获取dispatch方法 // 在组件内部使用state和dispatch // ... } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值