【React】知识点归纳:使用redux来编写一个简单的应用

react

使用redux来编写一个简单的应用

  • 一、使用redux编写
      • 效果
      • 下载依赖包
      • 源代码
      • 注意

  • 二、使用react编写
      • 效果
      • 源代码

  • 存在问题

一、使用redux编写

效果


下载依赖包

npm install --save redux

源代码

components/app.js

/*

应用组件

*/

import React, {Component} from 'react'

import PropTypes from 'prop-types'

import * as actions from '../redux/actions'

export default class App extends Component {

static propTypes = {

store: PropTypes.object.isRequired,

}

increment = () => {

const number = this.refs.numSelect.value * 1

this.props.store.dispatch(actions.increment(number))

}

decrement = () => {

const number = this.refs.numSelect.value * 1

this.props.store.dispatch(actions.decrement(number))

}

incrementIfOdd = () => {

const number = this.refs.numSelect.value * 1

let count = this.props.store.getState()

if (count % 2 === 1) {

this.props.store.dispatch(actions.increment(number))

}

}

incrementAsync = () => {

const number = this.refs.numSelect.value * 1

setTimeout(() => {

this.props.store.dispatch(actions.increment(number))

}, 1000)

}

render() {

return (

<div>

<p>

click {this.props.store.getState()} times {' '}

</p>

<select ref="numSelect">

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

</select>{' '}

<button onClick={this.increment}>+</button>

{' '}

<button onClick={this.decrement}>-</button>

{' '}

<button onClick={this.incrementIfOdd}>increment if odd</button>

{' '}

<button onClick={this.incrementAsync}>increment async</button>

</div>

)

}

}

redux/action-types.js

/*

Action对象的type常量名称模块

*/

export const INCREMENT = 'increment'

export const DECREMENT = 'decrement'

redux/actions.js

/*

action creator模块

*/

import {INCREMENT, DECREMENT} from './action-types'

export const increment = number => ({type: INCREMENT, number})

export const decrement = number => ({type: DECREMENT, number})

redux/reducers.js

/*

根据老的state和指定action, 处理返回一个新的state

*/

import {INCREMENT, DECREMENT} from './action-types'

export function counter(state = 0, action) {

console.log('counter', state, action)

switch (action.type) {

case INCREMENT:

return state + action.number

case DECREMENT:

return state - action.number

default:

return state

}

}

index.js

import React from 'react'

import ReactDOM from 'react-dom'

import {createStore} from 'redux'

import App from './components/app'

import {counter} from './redux/reducers'

// 根据counter函数创建store对象

const store = createStore(counter)

// 定义渲染根组件标签的函数

const render = () => {

ReactDOM.render(

<App store={store}/>,

document.getElementById('root')

)

}

// 初始化渲染

render()

// 注册(订阅)监听, 一旦状态发生改变, 自动重新渲染

store.subscribe(render)

注意

  • 牢记store的对象功能
    getState(): 得到 state

    dispatch(action): 分发 action, 触发 reducer 调用, 产生新的 state

    subscribe(listener): 注册监听, 当产生了新的 state 时, 自动调用

  • 这里的注册监听subscribe(listener)写在index.js中,一旦状态发生改变, 自动重新渲染。

二、使用react编写

效果

同上

源代码

components/app.js

import React, {Component} from 'react'

export default class App extends Component {

state = {

count: 0

}

increment = () => {

const num = this.refs.numSelect.value*1

const count = this.state.count + num

this.setState({count})

}

decrement = () => {

const num = this.refs.numSelect.value*1

const count = this.state.count - num

this.setState({count})

}

incrementIfOdd = () => {

let count = this.state.count

if(count%2==1) {

const num = this.refs.numSelect.value*1

count += num

this.setState({count})

}

}

incrementAsync = () => {

setTimeout(() => {

const num = this.refs.numSelect.value*1

const count = this.state.count + num

this.setState({count})

}, 1000)

}

render () {

const {count} = this.state

return (

<div>

<p>

click {count} times {' '}

</p>

<select ref="numSelect">

<option value="1">1</option>

<option value="2">2</option>

<option value="3">3</option>

</select>{' '}

<button onClick={this.increment}>+</button>{' '}

<button onClick={this.decrement}>-</button>{' '}

<button onClick={this.incrementIfOdd}>increment if odd</button>{' '}

<button onClick={this.incrementAsync}>increment async</button>

</div>

)

}

index.js

import React from 'react'

import ReactDOM from 'react-dom'

import App from './components/app'

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

存在问题

  1. redux 与 react 组件的代码耦合度太高
  2. 编码不够简洁

以上是 【React】知识点归纳:使用redux来编写一个简单的应用 的全部内容, 来源链接: utcz.com/z/383575.html

回到顶部