React-redux

Redux

在这里插入图片描述

在这里插入图片描述

//indes.js
import React from 'react';
import ReactDOM from 'react-dom';
import Todolist from './Todolist';
ReactDOM.render(<Todolist />, document.getElementById('root'));
//Todolist.js
import React ,{ Component } from 'react'
import 'antd/dist/antd.css'; 
import { Input,Button ,List } from 'antd';
import store from './store/index.js'
import {
  getClick,
  getBtnClick,
  getDelete
} from './store/actionCreators'

class Todolist extends Component{
  constructor(props){
    super(props)
    this.state=store.getState()
   this.hanleBtnClick=this.hanleBtnClick.bind(this)
    this.handleClick=this.handleClick.bind(this)
    this.handleChangeBack=this.handleChangeBack.bind(this)
   
    store.subscribe(this.handleChangeBack)
    

  }
  render(){
    return(
      <div style={{marginTop:'10px',marginLeft:'10px'}}>
        <div>
          <Input 
          onChange={this.handleClick}
          value={this.state.inputValue}
           placeholer="ino" style={{ width:300 , marginRight:'10px'}}/>
          <Button type="primary"
            onClick={this.hanleBtnClick}
          >提交</Button>
          <List 
          style={{marginTop:'10px',width:'300px'}}
          size="large"
          bordered
          dataSource={ this.state.list }
          renderItem={(item,index) => <List.Item 
          onClick={this.handleDelete.bind(this,index)}
          
          >{item}</List.Item>} />
         </div>,
    </div>
    )
  }
  handleClick(e){
      const action=getClick(e.target.value);
      store.dispatch(action)
  }
  
  handleChangeBack() {
     this.setState(store.getState());
  }


 hanleBtnClick(index){
   const action=getBtnClick()
   store.dispatch(action)
 }

 handleDelete(index){
   const action=getDelete(index);
   store.dispatch(action);
 }

}

export default Todolist
//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;
//store/reducer.js
import{
  change_input_Value,
  btn_Click_AddtoList,
  click_Delete

} from './actionTypes.js'
const defaultState={
  inputValue:'',
  list:[1,2,3]
}

//reducer 可以接受state,但是不能修改state
export default(state=defaultState,action)=>{
  if(action.type === change_input_Value){
    const newState = JSON.parse(JSON.stringify(state));
    newState.inputValue = action.value;
    return newState;
  }
  if(action.type === btn_Click_AddtoList){
    const newState = JSON.parse(JSON.stringify(state));
    newState.list.push(newState.inputValue);
    newState.inputValue='';
    console.log('newstate',newState);
    return newState
  }
  if(action.type === click_Delete){
    const newState = JSON.parse(JSON.stringify(state));
    newState.list.splice(action.index,1);
    newState.list=newState.list;
    console.log('newstate',newState);
    return newState
  }
  return state;
}
//store/actionTypes.js
export const change_input_Value='change_input_Value'
export const btn_Click_AddtoList='btn_Click_AddtoList'
export const click_Delete='click_Delete'
// store/actionCreators.js
import{
  change_input_Value,
  btn_Click_AddtoList,
  click_Delete

} from './actionTypes.js'

export const getClick=(value)=>({

    type:change_input_Value,
    value

})

export const getBtnClick=()=>({
  type:btn_Click_AddtoList

})
export const getDelete=(index)=>({
  type:click_Delete,
  index
})
复习补充

1.store 要求必须唯一
2.只有store能够改变自己的内容
3.Reduce 必须是一个纯函数,给定固定的输入就一定有固定的输出,且不会有任何的副作用—(在使用new date() 时就不是一个纯函数,异步操作也不能有,对传入参数的修改同样不孕系)

核心API

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值