亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React+Antd+Redux實現(xiàn)待辦事件的方法

 更新時間:2019年03月14日 09:53:47   作者:乘風(fēng)破浪  
這篇文章主要介紹了React+Antd+Redux實現(xiàn)待辦事件的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

之前也是寫過一篇關(guān)于Redux的文章,來簡單理解一下Redux,以及該如何使用。今天我就來分享一個也是入門級別的,React+Redux+antd來實現(xiàn)簡單的待辦事件。同時也講講自己對Redux的理解。先來看一張圖吧:

我們簡單的比喻來讓我們更加好的理解Redux,我們這樣比喻(圖書館借書):
1.React Component:借書人
2.Action Creators:你要說你要借書這句話,肯定要說話吧,就是一句話:我要借書
3.Store:圖書館管理員
4.Reducer:圖書館管理員肯定不可能記得所有書,那么Reducer就是作為一本小冊子,供圖書館管理員查

通俗理解:我要借書,我要先說話,告訴圖書館管理員我要借書,當(dāng)圖書館管理員知道了之后,但是它不可能知道所有的書籍在哪里,所以需要一本小冊子去找,最后找到了之后,再送到你手上。
專業(yè)術(shù)語理解:(Component)要借書,我要先說話(Action ),告訴圖書館管理員(Store)我要借書,當(dāng)圖書館管理員知道了之后,但是它不可能知道所有的書籍在哪里,所以需要一本小冊子(Reducer)去找,最后找到了之后,再送到你(Component)手上。

當(dāng)你看圖覺得蒙的時候你再看看這個比喻是不是更好理解了?流程我們大概清楚了,我們就開始來看怎么寫這個待辦事項吧。
我們先來列一個提綱吧,屢清楚思路再寫代碼。
1.react component(todolist.js)
2.引入antd
3.寫store
4.寫reducer
5.寫action

大概就是上面的一些流程:

如何引入antd呢?

官方文檔:鏈接描述

文件目錄結(jié)構(gòu)如下:

創(chuàng)建文件之前,首先創(chuàng)建圖書館管理員(store),他不知道書具體在哪里,所以再創(chuàng)建小冊子(redux),給到圖書館管理員(store):

//src/redux/index.js
import {createStore} from 'redux';
import reducer from './reducer'

const store=createStore(reducer);


export default store;
//src/redux/reducer.js
const defaultState={
  inputValue:'',
  list:[1,2]
}
export default(state=defaultState,action)=>{
  return state;
}

*注釋:剛開始state,這里一定要給state賦一個初始值,才不會報錯

接下來你就可以,在todolist.js中用store.getState()獲取到store的值,我把他直接賦值給狀態(tài):

我先實現(xiàn)一個由Component發(fā)送action,store收到action,在由reducer接受處理,最后返回一個新的狀態(tài),Component接收顯示:

//src/redux/TodoList.js
import React from 'react';
import 'antd/dist/antd.css';
import { Input,Button,List} from 'antd';
import store from './index';
export default class TodoList extends React.Component{
  constructor(props){
    super(props);
    this.state=store.getState();
  }
  componentDidMount(){
    console.log(this.state);
  }
  handleChg=(e)=>{
    const action={
      type:'change_input_value',
      inputValue:e.target.value
    }
    store.dispatch(action);
  }
  render(){ 
    console.log(this.state)  
    return(
      <div style={{marginTop:"10px",marginLeft:"20px"}}>
        <Input placeholder="請輸入" style={{width:"400px",marginRight:"10px"}} onChange={this.handleChg} value={this.state.inputValue}/>
        </div>
      </div>
    );
  }
  
}

思路:我們通過input框中監(jiān)聽內(nèi)容變化發(fā)送action,reucer去處理

//src/redux/reducer.js
const defaultState={
  inputValue:'',
  list:[1,2]
}

export default(state=defaultState,action)=>{
  if(action.type==='change_input_value'){
    const newState=JSON.parse(JSON.stringify(state))
    newState.inputValue=action.inputValue;
    return newState;
  }
  
  return state;
}

你可以打印出newState看一下,你就會發(fā)現(xiàn)inputValue就是你輸入的值了。

接下來的就可以舉一反三了。

完整代碼:

///src/redux/index.js
import {createStore} from 'redux';
import reducer from './reducer'

const store=createStore(reducer);
///src/redux/reducers.js
export default store;

const defaultState={
  inputValue:'',
  list:[1,2]
}

export default(state=defaultState,action)=>{
  if(action.type==='change_input_value'){
    const newState=JSON.parse(JSON.stringify(state))
    newState.inputValue=action.inputValue;
    return newState;
  }
  if(action.type==='send_message'){
    const newState=JSON.parse(JSON.stringify(state))
    newState.list.push(newState.inputValue);
    newState.inputValue='';
    return newState;
  }
  if(action.type==='delete_message'){
    const newState=Object.assign({},state);
    newState.list.splice(action.index,1);
    return newState;
  }
  return state;
}
///src/redux/todoList.js
import React from 'react';
import 'antd/dist/antd.css';
import { Input,Button,List} from 'antd';
import store from './index';
const data=[
  1,2,3
];
export default class TodoList extends React.Component{
  constructor(props){
    super(props);
    this.state=store.getState();
    store.subscribe(this.F5)
  }
  componentDidMount(){
    console.log(this.state);
  }
  handleChg=(e)=>{
    const action={
      type:'change_input_value',
      inputValue:e.target.value
    }
    store.dispatch(action);
  }
  handleSend=()=>{
    const action={
      type:'send_message',
    }
    store.dispatch(action);
  }
  F5=()=>{
    this.setState(store.getState());
  }
  handleItem=(index)=>{
    const action={
      type:'delete_message',
      index:index
    }
    store.dispatch(action);
  }
  render(){ 
    console.log(this.state)  
    return(
      <div style={{marginTop:"10px",marginLeft:"20px"}}>
        <Input placeholder="請輸入" style={{width:"400px",marginRight:"10px"}} onChange={this.handleChg} value={this.state.inputValue}/>
        <Button type="primary" onClick={this.handleSend}>發(fā)送</Button>
        <div style={{width:"400px",marginTop:"10px"}}>
        <List
           bordered
           dataSource={this.state.list}
           renderItem={(item,index) => (<List.Item onClick={this.handleItem.bind(this,index)}>{item}</List.Item>)}/>
        </div>
      </div>
    );
  }
  
}
//index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import TodoList from './redux/TodoList';

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

這樣就實現(xiàn)了一個利用redux來實現(xiàn)簡單的待辦事項.

相信你如果寫完這個demo之后,肯定對Redux大致有了了解。如果自己在寫的過程中有什么疑惑,歡迎提出,我會給你解答。后期也會更新一些關(guān)于Redux的其他方面的知識。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React組件三大核心屬性State?props?Refs介紹

    React組件三大核心屬性State?props?Refs介紹

    組件實例的三大核心屬性是:State、Props、Refs。類組件中這三大屬性都存在。函數(shù)式組件中訪問不到?this,也就不存在組件實例這種說法,但由于它的特殊性(函數(shù)可以接收參數(shù)),所以存在Props這種屬性
    2023-02-02
  • React Antd中如何設(shè)置表單只輸入數(shù)字

    React Antd中如何設(shè)置表單只輸入數(shù)字

    這篇文章主要介紹了React Antd中如何設(shè)置表單只輸入數(shù)字問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • React Native之TextInput組件解析示例

    React Native之TextInput組件解析示例

    本篇文章主要介紹了React Native之TextInput組件解析示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 完美解決react-codemirror2?編輯器需點擊一下或者延時才顯示數(shù)據(jù)的問題

    完美解決react-codemirror2?編輯器需點擊一下或者延時才顯示數(shù)據(jù)的問題

    這篇文章主要介紹了react-codemirror2編輯器需點擊一下或者延時才顯示數(shù)據(jù)的問題,解決方法也很簡單,需要手動引入自動刷新的插件,配置一下參數(shù)就可以了,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • React diff算法的實現(xiàn)示例

    React diff算法的實現(xiàn)示例

    這篇文章主要介紹了React diff算法的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • useReducer?createContext代替Redux原理示例解析

    useReducer?createContext代替Redux原理示例解析

    這篇文章主要為大家介紹了useReducer?createContext代替Redux原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • React渲染機制超詳細(xì)講解

    React渲染機制超詳細(xì)講解

    React整個的渲染機制就是React會調(diào)用render()函數(shù)構(gòu)建一棵Dom樹,在state/props發(fā)生改變的時候,render()函數(shù)會被再次調(diào)用渲染出另外一棵樹,重新渲染所有的節(jié)點,構(gòu)造出新的虛擬Dom tree
    2022-11-11
  • React中10種Hook的使用介紹

    React中10種Hook的使用介紹

    Hook 是 React 16.8 的新增特性,本文主要介紹了10種Hook的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 一文詳解React類組件中的refs屬性

    一文詳解React類組件中的refs屬性

    react中的ref類似于vue中的ref,都是可以操作dom的,這篇文章我們通過一個demo來學(xué)習(xí)這個屬性,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下
    2023-08-08
  • 瀏覽器中視頻播放器實現(xiàn)的基本思路與代碼

    瀏覽器中視頻播放器實現(xiàn)的基本思路與代碼

    這篇文章主要給大家介紹了關(guān)于瀏覽器中視頻播放器實現(xiàn)的基本思路與代碼,并且詳細(xì)總結(jié)了瀏覽器中的音視頻知識,對大家的理解和學(xué)習(xí)非常有幫助,需要的朋友可以參考下
    2021-08-08

最新評論