react-redux实现数据共享

本文展示了如何在React应用中创建并管理UI组件,使用Redux进行状态管理。通过`person.jsx`和`counter.jsx`两个组件,演示了用户输入处理、数据提交及异步操作。同时,`store.js`中整合了Redux的reducer,并使用`redux-thunk`中间件处理异步操作。此外,还定义了相应的action创建函数。
摘要由CSDN通过智能技术生成

新增一个ui组件
在这里插入图片描述
person.jsx

import React, { Component } from 'react'
import { connect } from 'react-redux'
import {addp} from "../redux/actions/person"
import {nanoid} from 'nanoid'
 class person extends Component {
    submit=()=>{
        
        const pname=this.name.value
        const page=this.age.value
        if(page===""){
          alert("请输入年龄")
          return
        }else if(pname===""){
            alert("请输入姓名")
            return
        }
        else{
           const perobj={id:nanoid(),name:pname,age:page}
        this.props.addperson(perobj)
        this.name.value=''
        this.age.value=''
        console.log(this.props.persons); 
        }
        
        
    }

    render() {
        return (
            <div style={{ marginTop: "3%", marginLeft: "35%" }}>
                <h4>上面的和为{this.props.count}</h4>
                <input type="text" placeholder="请输入姓名" ref={c=>this.name=c} />
                <input type="number" placeholder="请输入年龄" ref={c=>this.age=c} />
                <button onClick={this.submit}>提交</button>
                <br />
                <ul>
                    {
                        this.props.persons.map((a)=>{
                            return <li key={a.id}> 姓名:{a.name}--年龄:{a.age} </li>
                        })
                    }
                </ul>
            </div>
        )
    }
}
 
export default connect (
    state=>({persons:state.rens,
             count:state.he 
     }),
    { addperson:addp}
)(person)

counter.jsx

import React, { Component } from 'react'
import { connect } from 'react-redux'
import {createIncrementAction,createDecrementAction,createDecrementybAction} from "../redux/actions/count"
 class counter extends Component {
    state = {
        // count: 0
    }
    increment = () => {
        // 获取对应ref的valu
        const { value } = this.sel
        this.props.uiincrement(value*1)
        // store.dispatch(createIncrementAction(value*1))
    }
    decrement = () => {
        // 获取对应ref的value
        const { value } = this.sel
        this.props.uidecrement(value*1)

        // store.dispatch(createDecrementAction(value*1))
    }
    incrementifodd = () => {
        // 获取对应ref的value
        const { value } = this.sel
        const count=this.props.conut
        if (count % 2 !== 0) {
            this.props.uiincrement(value*1)

        }

    }
    incrementyb = () => {
            // 获取对应ref的value
            const { value } = this.sel
        this.props.uiincrementifodd(value*1,500)

            // store.dispatch(createDecrementybAction(value*1,500))
            
    }
    render() {
        return (
            <div style={{ marginTop: "10%", marginLeft: "35%" }}>
                <h2>
                    所得到的结果是{this.props.conut}

                </h2>
                <h4>人数一共为{this.props.person}</h4>
                <select name="" id="" ref={c => this.sel = c}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>&nbsp;
                <button onClick={this.increment}>相加</button> &nbsp;
                <button onClick={this.decrement}>相减</button> &nbsp;
                <button onClick={this.incrementifodd}>结果为奇数时相加</button> &nbsp;
                <button onClick={this.incrementyb}>异步相加</button> &nbsp;
            </div>
        )
    }
}

export default connect(
    state=>({conut:state.he,
             person:state.rens.length  
    }),
    { uiincrement:createIncrementAction,
      uidecrement:createDecrementAction,
      uiincrementifodd:createDecrementybAction,
    }
)(counter)

store.js

import { createStore,applyMiddleware,combineReducers} from "redux";
import thunk from "redux-thunk";
import jsreducer from "./reducer/count"
import person from "./reducer/person";
//暴露Store

const allreducer =combineReducers({
  he:jsreducer,
  rens:person
})

export default createStore(allreducer,applyMiddleware(thunk))

在这里插入图片描述

import {INCREMENT,DECREMENT} from "../constants"
import store from "../store"
export const createIncrementAction=data=>({ type:INCREMENT,data})
export const createDecrementAction=data=>({ type:DECREMENT,data})
export const  createDecrementybAction=(data,time)=>{
    return()=>{
        setTimeout(() => {
            store.dispatch(createIncrementAction(data))
                  
              }, time);
    }
  
}

在这里插入图片描述

import {ADD} from "../constants"
export  const addp = data =>({type:ADD,data})

count.js

export default function jsreducer(preState, action) {
    // preState代表上一个返回值
    // action包含type和传过来的值data
    const { type, data } = action
    switch (type) {
        //如果传过来的type匹配上了就执行
        case 'increment':
            return preState + data
        case 'decrement':
            return preState - data
        default:
            return 0
    }
}

person.js

const firper = [{ id: '001', name: "tom", age: "18" }]
export default function (preState = firper, action) {
    const { type, data } = action
    switch (type) {
        case 'addperson':
            return [data, ...preState]
        default:
            return preState
    }
}

在这里插入图片描述

export const INCREMENT ='increment'
export const DECREMENT ='decrement'
export const ADD ='addperson'

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Redux是一个独立的JavaScript库,用于管理应用程序的状态。它提供了一个可预测的状态容器,可以在整个应用程序中共享。Redux通过单向数据流来管理状态,使得状态的变化变得可控和可预测。 React-ReduxRedux的官方绑定库,它提供了一些与React集成的功能,使得在React应用中使用Redux更加方便。React-Redux提供了一个Provider组件,它可以将Redux store传递给整个应用程序。它还提供了一个connect函数,它可以将Redux store中的状态映射到React组件的props中,使得React组件可以轻松地访问Redux store中的状态。 ### 回答2: Redux是一个用于JavaScript应用程序的状态容器,它提供了一个可预测且可维护的方式来管理应用程序的状态。Redux的核心概念是“单一数据源”,即将整个应用程序的状态存储在单一对象树中,并且任何组件都可以访问和修改该状态树的任意部分。 react-redux是一个与React紧密集成的Redux绑定库。它提供了一组React组件和API,使得使用ReduxReact应用程序中更加容易。 reduxreact-redux之间的关系可以理解为Redux是一种状态管理库,而react-reduxReduxReact之间的纽带。 具体来说,react-redux提供了两种主要的API:Provider和connect。 Provider是一个React组件,允许我们将应用程序的Redux存储连接到React组件树中的所有组件。在Provider组件内部,可以通过store属性传递Redux存储对象,使得所有组件都可以访问该存储。 connect是一个高阶组件,用于将React组件连接到Redux存储中的状态和操作。通过connect,我们可以在React组件中访问Redux状态,以及派发Redux操作。connect本质上是一个函数,它接收一个组件作为参数并返回一个新的连接了Redux存储的组件。 总之,reduxreact-redux之间的区别在于,redux是一个独立的状态管理库,而react-reduxReduxReact之间的桥梁,帮助React应用程序连接到Redux存储,并访问存储中的状态和操作。 ### 回答3: ReduxReact-Redux都是在React项目中使用的JavaScript库。Redux是一个JavaScript状态容器,用于管理应用程序中的所有状态。Redux允许将状态存储在一个单一的地方,以便在整个应用程序中共享该状态。React-ReduxReact的一个库,用于与Redux一起使用,以便在React组件中访问和更新Redux状态。 Redux通过store提供一个单一的状态树,包含了整个应用程序的状态。通过使用store中的action和reducer,Redux可以跟踪状态的所有更改。这可以帮助开发人员更容易地调试和管理代码。但是,使用Redux需要一定的时间和精力来管理各个状态,尤其在较大的代码库中尤其如此。 React-Redux库是Redux的一个扩展,它提供了一组工具来帮助React组件访问和更新Redux状态。通过提供Provider组件,React-Redux使得Redux存储的状态可以传递到整个应用程序中的所有组件。通过使用connect函数和mapStateToProps和mapDispatchToProps参数,React-Redux允许开发人员将Redux状态映射到React组件中。这样,开发人员就可以根据需要将Redux状态作为props传递给组件,并且可以更方便地将状态更改传递回Redux store。 总之,ReduxReact提供了一个易于管理的状态储存架构,以帮助应用程序开发人员管理和跟踪应用程序状态。React-Redux是一组工具,它使开发人员可以更方便地在React组件中使用Redux,从而帮助开发人员更快地开发应用程序。两者的区别在于Redux是包含整个应用程序状态的状态容器,而React-Redux则是提供了一组工具,以帮助开发人员更方便地在React组件中使用Redux状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万事胜意sy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值