react-redux的简单使用

正式接触react应该有两个月了,项目开始也有一个月了,
开始新项目的时候就没有打算使用redux,因为感觉学习这个的成本挺高.
这两天,项目第一版已经出来了,昨天下午有时间就抓紧时间看看redux相关的东西,大致有个了解,然后今天就照葫芦画瓢,把一个模块给整理了下,换成了redux.
在这里记录下使用,以备后需.

react-redux

使用前得需要安装

npm i --save react-redux redux redux-thunk
  1. 先创建actions
    获取用户列表的相关操作.
// src/actions/users/index.js
import * as usersTypes from '../../constants/users/ActionTypes';

export const queryUsersSuccess = users => ({ type: usersTypes.QUERY_USERS_SUCCESS, users })

export const queryUsers = () => {
  return dispatch => {
    MyFetch.get(`v1/users`).then(data => {
      dispatch(queryUsersSuccess(data.users))
    })
  }
}
// MyFetch.js
import { message } from 'antd';
const API_URL = process.env.REACT_APP_DEV_API_URL
var Fetch = {
    get(path) {
        return new Promise((resolve, reject) => {
            fetch(`${API_URL}/${path}`, {
                headers: new Headers({
                    'token': localStorage.getItem(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React-Redux 是为了更方便地在 React 应用中使用 Redux 而创建的一个库。它提供了一些 API,可以让 Redux 的 store 和 React 组件之间更容易地进行连接。下面是 React-Redux使用方法: 1. 安装 React-Redux 首先,需要安装 React-Redux 库。在命令行中输入以下命令: ``` npm install react-redux ``` 2. 创建 Store 在应用中创建 Redux 的 store,可以使用 Redux 的 createStore 函数来创建,如下所示: ``` import { createStore } from 'redux'; import rootReducer from './reducers'; const store = createStore(rootReducer); ``` 3. Provider 组件 使用 React-Redux 提供的 Provider 组件将 store 传递给应用的所有组件。在应用的入口文件中,将 App 组件包裹在 Provider 组件中,如下所示: ``` import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import store from './store'; import App from './App'; ReactDOM.render( <Provider store={store}> <App /> </Provider>, document.getElementById('root') ); ``` 4. connect 函数 使用 React-Redux 提供的 connect 函数将组件连接到 store 上,使其能够读取和修改 store 中的状态。connect 函数接受两个参数:mapStateToProps 和 mapDispatchToProps。mapStateToProps 是一个函数,它将 store 中的状态映射到组件的 props 中。mapDispatchToProps 是一个函数,它将 store 的 dispatch 方法映射到组件的 props 中,使组件能够触发 action。下面是一个使用 connect 函数的示例: ``` import React from 'react'; import { connect } from 'react-redux'; class MyComponent extends React.Component { render() { return ( <div> <h1>{this.props.count}</h1> <button onClick={this.props.increment}>Increment</button> </div> ); } } const mapStateToProps = state => { return { count: state.count }; }; const mapDispatchToProps = dispatch => { return { increment: () => dispatch({ type: 'INCREMENT' }) }; }; export default connect(mapStateToProps, mapDispatchToProps)(MyComponent); ``` 在上面的代码中,connect 函数将 MyComponent 组件连接到 store 上,并将 count 属性和 increment 方法映射到组件的 props 中。这样,MyComponent 组件就能够读取和修改 store 中的状态了。 以上就是使用 React-Redux 的基本方法。通过 Provider 和 connect 这两个 API,我们可以更方便地在 React 应用中使用 Redux。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值