React Redux 基础配置

目录

store

--index.js

--reducer.js

--actionTypes.js

--actionCreator.js 

-TodoList.js

-TodoItem.js

-index.js


store

--index.js

import { createStore } from 'redux';
import reducer from './reducer'
const store = createStore(
    reducer,
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)

export default store;

--reducer.js

import { INPUT_VALUE_CHANGE, LIST_ITEM_CHANGE, LIST_ITEM_DEL } from './actionTypes';

const defaultState = {
    inputValue: '',
    list: []
}
export default (state = defaultState, action) => {
    if(action.type === INPUT_VALUE_CHANGE){
        const newState = JSON.parse(JSON.stringify(state));
        newState.inputValue = action.value
        return newState;
    }
    if(action.type === LIST_ITEM_CHANGE){
        const newState = JSON.parse(JSON.stringify(state));
        newState.inputValue = '';
        newState.list.push(state.inputValue)
        return newState;
    }
    if(action.type === LIST_ITEM_DEL){
        const newState = state;
        newState.list.splice(action.value,1);
        return newState;
    }
    return state;
}

--actionTypes.js

export const INPUT_VALUE_CHANGE = 'input_value_change';
export const LIST_ITEM_CHANGE = 'list_item_change';
export const LIST_ITEM_DEL = 'list_item_del';

--actionCreator.js 

import { INPUT_VALUE_CHANGE, LIST_ITEM_CHANGE, LIST_ITEM_DEL } from './actionTypes';


export const inputValueChangeAction = (value) => ({
    type: INPUT_VALUE_CHANGE,
    value
});

export const listItemChangeAction = () => ({
    type: LIST_ITEM_CHANGE
});

export const listItemDelAction = (value) => ({
    type: LIST_ITEM_DEL,
    value
});

-TodoList.js

import React, { Component, Fragment } from 'react';
import TodoItem from './TodoItem';
import { Button, Input } from 'antd'
import store from './store';
import { inputValueChangeAction, listItemChangeAction } from './store/actionCreator';

class TodoList extends Component {
  constructor(props) {
    super(props);
    this.state = store.getState();
    this.handleInputChange = this.handleInputChange.bind(this);
    this.handleAdd = this.handleAdd.bind(this);
    this.handleStoreChange = this.handleStoreChange.bind(this);
    store.subscribe(this.handleStoreChange);
  }

  render() {
    return (
      <Fragment>
        <div>
          <Input
            style={{ width: '300px' }}
            value={this.state.inputValue}
            placeholder="Input Something"
            onChange={this.handleInputChange}
          />
          <Button className="blue" onClick={this.handleAdd} type="primary">添加</Button>
        </div>
        <TodoItem/>
      </Fragment>
    )
  }

  handleInputChange(e) {
    const action = inputValueChangeAction(e.target.value);
    store.dispatch(action);
  }
  handleAdd() {
    if (this.state.inputValue === '') {
      alert('Please Input Something');
      return false;
    }
    const action = listItemChangeAction();
    store.dispatch(action);
  }
  handleStoreChange(){
    this.setState(store.getState());
  }
}
export default TodoList;

-TodoItem.js

import React, { Fragment, Component } from 'react';
import { List,Button } from 'antd';
import store from './store';
import { listItemDelAction } from './store/actionCreator';

class TodoItem extends Component {
  constructor(props) {
    super(props);
    this.state = store.getState();
    this.handleStoreChange = this.handleStoreChange.bind(this);
    store.subscribe(this.handleStoreChange);
  }
  shouldComponentUpdate() {
    return true;
  }
  render() {
    return (
      <Fragment>
        <List
          style={{width:"364px",marginTop:"20px"}}
          bordered
          dataSource={this.state.list}
          renderItem={(item,index) => (
            <List.Item>
            <span style={{padding:"5px"}}>{item}</span>
            <Button type="danger" onClick={this.handleDel.bind(this,index)}>删除</Button>
            </List.Item>
          )}
        />
      </Fragment>
    )
  }
  handleStoreChange(){
    this.setState(store.getState());
  }
  handleDel(index) {
    const action = listItemDelAction(index);
    store.dispatch(action);
  }
}

export default TodoItem;

-index.js

import React from 'react';
import ReactDOM from 'react-dom';
import TodoList from './TodoList';
import 'antd/dist/antd.css';
import './common.css';


ReactDOM.render(<TodoList />, document.getElementById('root'));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值