react-redux connect的使用

actions

counter

const increment1 = ()=> {
    return {
        type: 'INCREMENT_COUNTER',
    }
};
const decrement1 = ()=> {
    return {type: 'DECREMENT_COUNTER'}
};

module.exports = {
    increment1,
    decrement1
};

infoMember

const setInfoMember = (payload) => {
    return {
        type: 'SET_MEMBER_INFO',
        payload
    }
}

const changeInfo = (payload) => {
    return {
        type: 'CHANGE_INFO',
        payload
    }
}

module.exports = {
    setInfoMember,
    changeInfo
}

reducers

counter

export default function counter(state = 0, action) {
    switch (action.type) {
        case 'INCREMENT_COUNTER':
            return state + 1;
        case 'DECREMENT_COUNTER':
            return state - 1;
        default:
            return state
    }
};

infoMember

export default function infoMember(state = {}, action) {
    let _state = Object.assign({}, state);
    switch (action.type) {
        case 'SET_MEMBER_INFO':
            return action.payload;
        case 'CHANGE_INFO':
            _state.script = action.payload;
            return _state;
        default:
            return state;
    }
}

index.js

import { combineReducers } from 'redux'
import counter from './counter'
import infoMember from './infoMember'

//使用redux的combineReducers方法将所有reducer打包起来
const rootReducer = combineReducers({
    counter,
    infoMember
});

export default rootReducer;

App.js

import { connect } from 'react-redux'
import actions from '../actions/counter';
import actions2 from '../actions/infoMember';

import React, {Component} from 'react'

class Counter extends Component {
    constructor(props) {
        super(props);
        this.add = this.add.bind(this);
        this.reduce = this.reduce.bind(this);
        this.changeInfo = this.changeInfo.bind(this);
    }
    componentDidMount() {
        console.log(this.props.infoMember); // {}
    }
    add() {
        this.props.dispatch(actions.increment1());
        this.props.dispatch(actions2.setInfoMember({id: '9932939293', script: 'Try my best'}));
    }
    reduce() {
        this.props.dispatch(actions.decrement1());
    }
    changeInfo() {
        this.props.dispatch(actions2.changeInfo('Come on'));
    }
    render() {
        return (
            <div>
                <p>
                    Clicked: {this.props.counter} times
                    {' '}
                    <button onClick={this.add}>+</button>
                    {' '}
                    <button onClick={this.reduce}>-</button>
                    {' '}
                </p>
                <p onClick={this.changeInfo}>this.props.infoMember: {this.props.infoMember.id} - {this.props.infoMember.script}</p>
            </div>
        )
    }
}

// //将state.counter绑定到props的counter
// const mapStateToProps = (state) => {
//     return {
//         counter: state.counter
//     }
// };
// //将action的所有方法绑定到props上
// const mapDispatchToProps = (dispatch, ownProps) => {
//     return {
//         add: (...args) => dispatch(actions.increment1(...args)),
//         reduce: (...args) => dispatch(actions.decrement1(...args))
//     }
// };

//通过react-redux提供的connect方法将我们需要的state中的数据和actions中的方法绑定到props上
export default connect(state => (state))(Counter)

入口文件

import React from 'react';
import ReactDom from 'react-dom';
import {Provider} from 'react-redux';
import {createStore} from 'redux';
import reducer from './reducers/index.js';
import App from './containers/App.js';

let store = createStore(reducer);

ReactDom.render(
    <Provider store={store}>
        <App/>
    </Provider>,
    document.getElementById('root')
);

index.html

<!DOCTYPE html>
<html lang="en">
<head>Counter</head>
<body>
<div id="root">
</div>
<script type="text/javascript" src="bundle.js" charset="utf-8"></script>
</body>
</html>

app.js

const express = require('express');
const path = require('path');
const bodyParser  = require('body-parser');
var app = express();

app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(express.static(__dirname + '/public'));

app.use('/', function (req, res) {
    res.sendFile(path.resolve('./public/index.html'));
});

app.listen(3000, function () {
    console.log('server started at http://localhost:3000'); // eslint-disable-line no-console
});

module.exports = app;

webpack配置 webpack.config.js

var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    devtool: 'eval-source-map',//由bundle.js映射到origin文件,容易在浏览器调试
    entry: __dirname + "/public/src/main.js",
    output: {
        path: __dirname + '/public',
        filename: "bundle.js"
    },
    module: {
        /*test:  A regular expression that matches the file extensions that should run through this loader (Required).
         loader:  The name of the loader (Required).
         include / exclude:   Optional setting to manually set which folders and files the loader should explicitly add or ignore.
         query:  The query setting can be used to pass Additional options to the loader.
         */
        loaders: [
            {
                test: /\.(js|jsx)$/,//一个匹配loaders所处理的文件的拓展名的正则表达式,这里用来匹配js和jsx文件(必须)
                exclude: /node_modules/,//屏蔽不需要处理的文件(文件夹)(可选)
                loader: 'babel-loader',
                query: {
                    presets: ['es2015', 'react']
                }
            },
            {test: /\.css$/, loader: "style-loader!css-loader"},
            {test: /\.png$/, loader: "file-loader"},
            {test: /\.jpg$/, loader: "file-loader"}
        ]
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
react-redux是一个流行的用于在React应用中管理全局状态的库。Redux是JavaScript中一个用于状态管理的工具,它通过将状态存储在单一的全局状态树中,并使用纯函数来处理状态的变化,使得状态管理变得简单而可预测。 在React中使用Redux,通常需要使用react-redux库提供的connect函数。connect函数是一个高阶组件,用于连接React组件和Redux的状态管理机制。 connect函数的基本用法是将React组件作为参数传递给connect,并在返回的函数中配置所需的状态和动作。它接受两个参数:mapStateToProps和mapDispatchToProps。 mapStateToProps是一个函数,用于从Redux的全局状态树中选择所需的状态,并作为React组件的属性传递给组件。这样,组件可以通过props访问到所需的状态,并将其渲染到页面中。 mapDispatchToProps是一个函数,用于将Redux的动作绑定到组件的props上。这样,组件可以通过调用props中的方法来触发Redux中定义的动作。 connect函数还可以接收一个可选的第三个参数,用来配置其他属性,比如在组件未连接到Redux时是否抛出警告。 使用connect函数后,就可以将Redux的状态和动作与React组件连接起来,并使其能够访问和操作全局状态。这样,组件就可以根据状态变化来更新UI,并通过触发动作来改变全局状态。 总而言之,react-reduxconnect函数提供了一种简单而方便的方式来连接React组件与Redux的状态管理机制,使得在React应用中管理全局状态变得更加容易和可维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值