国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

規(guī)范化 Redux 使用

這篇具有很好參考價值的文章主要介紹了規(guī)范化 Redux 使用。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

安裝&配置 redux 和 react-redux

npm install --save redux react-redux

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { Provider } from 'react-redux'
import store from './store'

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <Provider store={store} >
      <App />
    </Provider>
  </React.StrictMode>
);

配置倉庫 store

src 目錄下 新建 store 文件夾,并創(chuàng)建 index.js 文件

import { legacy_createStore as createStore } from "redux";
import reducer from "./reducers/root.reducer";

const store = createStore(reducer)

export default store

store目錄下新建 reducers 文件夾

reducers 文件夾下創(chuàng)建 root.reducer.js 文件, modal.reducer.js 文件、todoList.reducer.js 文件

這里引入我們采用合并 reducer,以方便我們更好的使用 redux

modal.reducer.js

const defaultstate = {
  isShowModal: false
}

export default (state = defaultstate, action) => {
  switch (action.type) {
    case "changeModalShow":
      return {
        ...state,
        isShowModal: action.value
      }
    default:
      return state;
  }
}

todoList.reducer.js

const defaultstate = {
    list: [1,2,3,4,5,6]
}

export default (state = defaultstate, action) => {
    switch(action.type) {
        case "changeList":
            return {
                ...state,
                list: action.value
            }
        default:
            return state;
    }
}

combineReducers 方法可以讓我們將多個 reducer 文件合并

root.reducer.js

import { combineReducers } from 'redux'
import ModalReducer from './modal.reducer'
import TodoListReducer from './todoList.reducer'

export default combineReducers({
  modal: ModalReducer,
  todoList: TodoListReducer
})

頁面使用store

App.js

import React from 'react';
import './App.css';
import TotoList from './components/TotoList';
import Modal from './components/Modal';

function App() {
  return (
    <div className="App">
      <TotoList />
      <Modal />
    </div>
  )
}

export default App;

src 目錄下新建 components 文件夾

創(chuàng)建 TotoList.js、 Modal.js 兩個文件

Modal.js

import React from 'react'

const Modal = () => {

  const styles = {
    width: '400px',
    height: '400px',
    left: '50%',
    top: '50%',
    position: 'absolute',
    transform: 'translate(-50%, -50%)',
    background: 'aliceblue',
    display: 'block'
  }

  return (
    <div>
      <button>顯示</button>
      <button>隱藏</button>
      <div style={styles}></div>
    </div>
  )
} 

export default Modal
引入 connect
import { connect } from 'react-redux'
export default connect(mapStateToProps, mapDispatchToProps)(Modal)
配置 mapStateToProps
const mapStateToProps = state => ({
  isShowModal: state.modal.isShowModal
})
配置 mapDispatchToProps

store 文件下 創(chuàng)建 const 文件夾,并創(chuàng)建 modal.const.js 文件

modal.const.js

export const CHANGEMODALSHOW = 'changeModalShow'

store 文件下 創(chuàng)建 actions 文件夾,并創(chuàng)建 modal.actions.js 文件

import { CHANGEMODALSHOW } from '../const/modal.const'

export const changeModalShow = value => ({type: CHANGEMODALSHOW, value})

Modal.js

import React from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import * as modalActions from '../store/actions/modal.actions'

const Modal = ({isShowModal, changeModalShow}) => {

  const styles = {
    width: '400px',
    height: '400px',
    left: '50%',
    top: '50%',
    position: 'absolute',
    transform: 'translate(-50%, -50%)',
    background: 'aliceblue',
    display: isShowModal ? 'block' : 'none'
  }

  const handelShowModal = () => {
    changeModalShow(true)
  }

  const handelHiddenModal = () => {
    changeModalShow(false)
  }

  return (
    <div>
      <button onClick={handelShowModal}>顯示</button>
      <button onClick={handelHiddenModal}>隱藏</button>
      <div style={styles}></div>
    </div>
  )
} 

const mapStateToProps = state => ({
  isShowModal: state.modal.isShowModal
})

const mapDispatchToProps = dispatch => bindActionCreators(modalActions, dispatch)

export default connect(mapStateToProps, mapDispatchToProps)(Modal)

todoList.js 的寫法一樣的文章來源地址http://www.zghlxwxcb.cn/news/detail-703176.html

到了這里,關于規(guī)范化 Redux 使用的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 數(shù)據(jù)清洗與規(guī)范化詳解

    數(shù)據(jù)清洗與規(guī)范化詳解

    數(shù)據(jù)處理 流程,也稱數(shù)據(jù)處理管道,是將原始數(shù)據(jù)轉化為有意義的信息和知識的一系列操作步驟。它包括數(shù)據(jù) 采集 、 清洗 、 轉換 、 分析 和 可視化 等環(huán)節(jié),旨在提供有用的見解和決策支持。 那我常用的山海鯨可視化軟件舉例,在數(shù)據(jù)可視化中數(shù)據(jù)處理是可視化展示前非

    2024年02月07日
    瀏覽(21)
  • PyTorch 中的批量規(guī)范化

    批量規(guī)范化(Batch Normalization)是深度學習中一種常用的技術,用于加速訓練過程并提高模型的穩(wěn)定性和泛化能力。以下是PyTorch中批量規(guī)范化的一些關鍵知識點: 1.nn.BatchNorm1d 和 nn.BatchNorm2d: 2.PyTorch提供了nn.BatchNorm1d用于在全連接層后應用批量規(guī)范化,以及nn.BatchNorm2d用于在卷

    2024年01月15日
    瀏覽(26)
  • uniapp接口請求api封裝,規(guī)范化調用

    uniapp接口請求api封裝,規(guī)范化調用

    封裝規(guī)范和vue中的差不多,都是統(tǒng)一封裝成一個request對象,然后在api.js里面調用。 先創(chuàng)建一個utils文件夾,然后里面創(chuàng)建一個request.js,代碼如下: 在api文件夾中封裝對應的index.js文件,然后導入request對象: 在對應的vue或者react中引入并調用:

    2024年02月08日
    瀏覽(20)
  • 【第一章 先導篇】1. 規(guī)范化的學習模型

    舉例:什么是編碼?

    2024年04月25日
    瀏覽(22)
  • idea的git的規(guī)范化提交插件

    idea的git的規(guī)范化提交插件

    1.在idea中安裝git的插件git commit Template插件 打開IDEA-選擇菜單欄的File-Settings,選擇Plugins-MarkPlace輸入Git Commit Template進行搜索,點擊apply, 2.在日常commit的?時候按照如下操作進行:在commit的頁面,點擊下圖的圖標后,根據(jù)實際情況選擇或者輸入相關內容,該插件會根據(jù)其填入的內

    2024年02月12日
    瀏覽(21)
  • 在 Visual Studio 中規(guī)范化文件編輯

    在 Visual Studio 中規(guī)范化文件編輯

    生成了對應的 .editorconfig 文件,存放在倉儲的根目錄。即對整個倉儲所有的用 VS 作為 IDE 編輯的項目生效。 同時支持子目錄有自己的 .editorconfig 文件,可以選擇繼承更高級別父目錄的配置,也可以不繼承。 該文件將應用到模板項目當中。除非特殊情況需根據(jù)項目定制編碼規(guī)

    2024年02月17日
    瀏覽(23)
  • React項目規(guī)范:目錄結構、根目錄別名、CSS重置、路由、redux、二次封裝axios

    先創(chuàng)建項目: create-react-app 項目名 ,然后換個圖標,換個標題 配置 jsconfig.json :這個文件在Vue通過腳手架創(chuàng)建項目時自動生成, React是沒有自動生成, jsconfig.json 是為了讓vs code的代碼提示更友好, 按需求決定是否配置; 對 src 文件夾的目錄進行初始化: 1、安裝craco: npm install

    2023年04月08日
    瀏覽(15)
  • 【前端】React快速入門+Redux狀態(tài)管理

    【前端】React快速入門+Redux狀態(tài)管理

    本文旨在記錄react的基礎內容,幫助有需要的同學快速上手,需要進一步了解描述更加穩(wěn)妥和全面的信息,請查閱官方文檔 官方文檔點擊這里進行跳轉 react框架 vue,react,angular這幾種主流前端框架使用頻率較高…本質還是js庫。 React.js是一個用于構建用戶界面的JavaScript庫。它由

    2024年02月12日
    瀏覽(60)
  • 數(shù)據(jù)庫期末復習(10)數(shù)據(jù)庫規(guī)范化理論

    數(shù)據(jù)庫期末復習(10)數(shù)據(jù)庫規(guī)范化理論

    ?函數(shù)依賴(概念):FD 范式分解(評估準則): 模式分解(工具): 如何衡量一個數(shù)據(jù)庫好不好:準確 高效 如果一個數(shù)據(jù)庫設計的不好的話的,會帶來哪些問題 刪除異常 數(shù)據(jù)冗余 為什么會導致出現(xiàn)上方的問題:數(shù)據(jù)依賴 數(shù)據(jù)依賴的分類:完全依賴,部分依賴,傳遞依賴和相應的定義 A

    2024年02月08日
    瀏覽(21)
  • 【數(shù)據(jù)庫原理 ? 四】數(shù)據(jù)庫設計和規(guī)范化理論

    【數(shù)據(jù)庫原理 ? 四】數(shù)據(jù)庫設計和規(guī)范化理論

    前言 數(shù)據(jù)庫技術是計算機科學技術中發(fā)展最快,應用最廣的技術之一,它是專門研究如何科學的組織和存儲數(shù)據(jù),如何高效地獲取和處理數(shù)據(jù)的技術。它已成為各行各業(yè)存儲數(shù)據(jù)、管理信息、共享資源和決策支持的最先進,最常用的技術。 當前互聯(lián)網(wǎng)+與大數(shù)據(jù),一切都建立

    2023年04月12日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包