当前位置:网站首页>Redux principle
Redux principle
2022-07-21 01:33:00 【venture to ask】
react The data flow of component communication in is one-way , Only outer components can transmit data to lower components , But the lower components cannot be passed to the upper components , If the lower component needs to pass values to the upper component , You need the upper component to pass the method of modifying data to the lower component
redux The purpose is to solve react The way components communicate in
redux Subscription publishing mode , All subscriptions redux The components of , once redux The state of state Change , Then all subscriptions state Will be re rendered
redux It can be used in any frame
install npm install redux
redux principle
import {createStore} from 'redux'
console.log(createStore) ---ƒ createStore(reducer, preloadedState, enhancer)
createStore Production container ( object ) Methods , Return value is an object The argument is a function
createStore Returned object :{dispatch: ƒ, subscribe: ƒ, getState: ƒ, replaceReducer: ƒ, @@observable: ƒ}
- Definition store console.log(store)
- dispatch: distributed , For behavior
- subscribe: subscribe Subscription build
- getStore: Read state obtain state
Definition reducer The first parameter state The second parameter action
Definition state
Definition action
app.js in Implement a counter
import React from 'react';
import ReactDOM from 'react-dom/client';
import {createStore} from 'redux'
import './index.css';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
function App (){
return (
<div>
<h1>redux</h1>
<button onClick={()=>{store.dispatch(increment)}}> increase </button>
<p>{store.getState().count}</p>
<button onClick={()=>{store.dispatch(decrement)}}> Reduce </button>
</div>
)
}
// 4. Definition action
let increment = {type:'increment'}
let decrement = {type:'decrement'}
// 3. Definition state
let initstate = {
count:0
}
//2. Definition reducer The first parameter state The second parameter action
function reducer(state = initstate,action){
switch(action.type){
case 'increment' :return {count:state.count + 1}
break;
case 'decrement' :return {count:state.count - 1}
break;
default :return state
}
}
//1. Definition store
let store = createStore(reducer)
console.log(store)
// subscribe
store.subscribe(() => {
root.render(
<React.StrictMode>
<App></App>
</React.StrictMode>
);
console.log(store.getState())
})
root.render(
<React.StrictMode>
<App></App>
</React.StrictMode>
);
reportWebVitals();
边栏推荐
猜你喜欢
随机推荐
十分钟生成影视级室内设计效果,红星美凯龙设计云如何升级传统家居行业
Network security - comprehensive penetration test -cve-2019-15107-webmin remote code execution
力扣刷题61. 旋转链表
LeetCode.745. 前缀和后缀搜索____双字典树+双指针
Iptables prevent nmap port scanning
Tutorial on principles and applications of database systems (029) -- data integrity of MySQL (II): defining primary keys
PHP(1)
On the surface, the development logic of the meta universe and the Internet is the same, but in fact, it is not
Luogu p1873 [coci 2011/2012 5] Eko / tree cutting dichotomy
剑指 Offer II 041. 滑动窗口的平均值_____使用队列 / 循环数组实现
In the last interview, I knelt on redis, finished the internal redis documents given by cousin Ali, and finally entered the big factory
Understand the MySQL architecture design in one article, and don't worry about the interviewer asking too much
网络安全—综合渗透测试-CVE-2017-12629-Apache Solr远程代码执行
暑假安全第二次作业
网络安全—综合渗透测试-CVE-2010-2883-PDF漏洞分析
网络安全——信息隐藏-使用隐写术来防止敏感数据被盗用
数据库系统原理与应用教程(031)—— MySQL 的数据完整性(四):定义外键(FOREIGN KEY)
cannot import name ‘import_ String 'from' Werkzeug '[bug resolution]
DNS解析过程
I want to display the number of records in the SQL database table on the label label