当前位置:网站首页>Introduction to Redux: initial experience of Redux
Introduction to Redux: initial experience of Redux
2022-06-30 05:12:00 【Catci】
List of articles
preparation
1. structure React project
npx create-react-demo redux-app
2. stay Pages Under the table of contents , establish home Components
cd src;mkdir pages/home;cd . > pages/home/index.js
modify pages/home/index.js
import React from 'react'
export default class Home extends React.Component {
render(){
return (
<button>Press me,send an action</button>
)
}
}
3. stay App.js Introduce this component in
import React from 'react';
import logo from './logo.svg';
import './App.css';
import Home from './pages/home'
function App() {
return (
<div className="App">
<Home/>
</div>
);
}
export default App;
Building objects
structure Action
- Create a folder in the root directory action
- Create... In this directory index.js file , Used to build Action
- stay action Create a function inside , utilize return, Return to one action object , Be careful to carry type attribute
- Put this action Create function export
const sendAction=()=>{
return{
type:"send_type",
value:'i am an action'
}
}
module.exports = sendAction
structure Reducer
- Create a folder in the root directory reducer
- Create... In this directory index.js file , Used to build Reducer, Receive two parameters
- The first parameter is state, Define initialization state, assignment
- Function to determine the second parameter action Of type Whether the value is what we sent
- If it is , Return to new state
- hold reducer Export
const initState = {
value:'default'
}
const reducer = (state=initState,action) => {
console.log("ruducer:",state,action)
switch (action.type) {
case 'send_type':
return Object.assign({
},state,action);
default:
return state;
}
}
module.exports = reducer
structure store
- Create a folder in the root directory store
- Create... In this directory index.js,
- Import the reducer
- createStore The return value of is built store, export
import {
createStore } from 'redux'
import reducer from '../reducer'
const store = createStore(reducer)
export default store
Use
1. For the page button Button to bind a click event
import React from 'react'
import store from '../../store'
import sendAction from '../../action'
export default class Home extends React.Component {
handleClick = () => {
const action = sendAction()
store.dispatch(action)
}
render(){
return <button onClick={
this.handleClick}>Press me,send an action</button>
}
}
In order to carry on console notice state and action, modify reducer as follows
const initState = {
value:'default'
}
const reducer = (state=initState,action) => {
console.log("ruducer:",state,action)
switch (action.type) {
case 'send_type':
return Object.assign({
},state,action);
default:
return state;
}
}
module.exports = reducer
2. Once the component is loaded , adopt store Register the listener , The return value can be used to log off listening
import React from 'react'
import store from '../../store'
import sendAction from '../../action'
export default class Home extends React.Component {
handleClick = () => {
const action = sendAction()
store.dispatch(action)
}
componentDidMount(){
store.subscribe(()=>{
console.log("Subscribe:",store.getState())
})
}
render(){
return (
<>
<button onClick={
this.handleClick}>Press me,send an action</button>
<div>{
store.getState().value}</div>
</>
)
}
}
3. In the click event handler , adopt store.dispatch To send a action
Make the page appear state Return result of
import React from 'react'
import store from '../../store'
import sendAction from '../../action'
export default class Home extends React.Component {
handleClick = () => {
const action = sendAction()
store.dispatch(action)
}
componentDidMount(){
store.subscribe(()=>{
console.log("Subscribe:",store.getState())
this.setState({
})
})
}
render(){
return (
<>
<button onClick={
this.handleClick}>Press me,send an action</button>
<div>{
store.getState().value}</div>
</>
)
}
}
At this point you can start Project preview effect
边栏推荐
- Unity application class and data file path
- LxC and LXD container summary
- 虚析构和纯虚析构
- Very nervous. What should I do on the first day of software testing?
- Unity- the camera follows the player
- Unity packaging and publishing webgl error reason exception: failed building webgl player
- Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
- Chapter 12 pipeline monitoring of OpenGL super classic (version 7)
- JS 数组的排序 sort方法详解
- Unity supports the platform # define instruction of script
猜你喜欢

Unity C trigonometric function, right triangle corner calculation

East Tower attack and defense world - XSS bypasses the safety dog

ParticleSystem in the official Manual of unity_ Collision module
![[vcs+verdi joint simulation] ~ take the counter as an example](/img/fb/214a4e65c53503ecbc38a5e43523cf.png)
[vcs+verdi joint simulation] ~ take the counter as an example

Unity2019.3.8f1 development environment configuration of hololens2

14x1.5cm竖向标签有点难,VFP调用BarTender来打印

LXC 和 LXD 容器总结

One command to run rancher

Some books you should not miss when you are new to the workplace

Procedural animation -- inverse kinematics of tentacles
随机推荐
Pytorch的安装以及入门使用
Force buckle 59 Spiral matrix II
PWN入门(2)栈溢出基础
Unity C trigonometric function, right triangle corner calculation
Under what conditions does the Z-index attribute expire?
Connect() and disconnect() of socket in C #
Basic operations of Oracle data
Pit of smoothstep node in shadergraph
003-JS-DOM-Attr-innerText
Untiy3d controls scene screenshots through external JSON files
Revit二次開發---未打開項目使用面板功能
Chapter 11 advanced data management of OpenGL super classic (version 7)
RedisTemplate 常用方法汇总
Ripple effect of mouse click (unity & shader)
《谁动了我的奶酪》读后感
[vcs+verdi joint simulation] ~ take the counter as an example
Operation file file class method
Sourcetree usage
Nestjs中控制器和路由的配置使用
Unity download and installation website