react(尚硅谷)08-react-redux,连接容器组件和redux组件、provider组件的使用、整合ui组件和容器组件,redux开发者工具

1、react-redux

是一个插件库
在这里插入图片描述

2、连接容器组件和redux组件

安装react-redux库

npm i react-redux --save

1、创建容器文件夹container,并创建响应的容器组件count,及index.js
在这里插入图片描述
2、container/count/index.js

//引入count的ui组件
import CountUI from '../../component/Count'
import {
  createIncreatmentAciton,
  createDecreatmentAciton,
  createDecreatmentAsyncAciton
} from '../../redux/count_action'
//引入connect用于连接ui组件与redux
import  {connect} from 'react-redux'

//mapStateToProps函数返回的对象中的key就作为传递给ui组件props的key,value就作为传递给ui组件props的value-状态
function mapStateToProps (state) {
  return {count:state}
}
//mapDispatchToProps函数返回的对象中的key就作为传递给ui组件props的key,value就作为传递给ui组件props的value-操作状态
function mapDispatchToProps (dispatch) {
  return {
    jia:(number)=>{
    //通知redux执行加法
      dispatch(createIncreatmentAciton(number))
    },
    jian:(number)=>{
      //通知redux执行加法
      dispatch(createDecreatmentAciton(number))
    },
    jiaAsync:(number,time)=>{
      //通知redux执行加法
      dispatch(createDecreatmentAsyncAciton(number,time))
    }
  }
}

//获得一个容器组件
export default connect(mapStateToProps,mapDispatchToProps)(CountUI)


3、component/count/index.js

import React, { Component } from 'react'
// //引入store中保存的状态
// import store from '../../redux/store'
// //引入actionCreator,专门用于创建action对象
// import {
//   createDecreatmentAciton,
//   createIncreatmentAciton,
//   createDecreatmentAsyncAciton
// } from '../../redux/count_action'


class Count extends Component {

  //加法
  increament=()=>{
    //函数体
    const {value}=this.selectNumber
    this.props.jia(value*1)
  }
  //减法
  decreament=()=>{
    //函数体
    const {value}=this.selectNumber
    this.props.jian(value*1)
  }
  //奇数在加
  increamentIfOdd=()=>{
    //函数体
    const {value}=this.selectNumber
    if (this.props.count % 2 ===1){
      this.props.jian(value*1)
    }
  }
  increamentAsync=()=>{
    //函数体
    const {value}=this.selectNumber
    this.props.jiaAsync(value*1,500)

  }
  render () {
    console.log(this.props)
    return (
      <div>
        <h1>当前求和为:{this.props.count}</h1>
        <select ref={currentNode=>this.selectNumber=currentNode}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>&nbsp;
        <button onClick={this.increament}>+</button>&nbsp;
        <button onClick={this.decreament}>-</button>&nbsp;
        <button onClick={this.increamentIfOdd}>当前求和我技术再加</button>&nbsp;
        <button onClick={this.increamentAsync}>异步+</button>&nbsp;
      </div>
    )
  }
}

export default Count

4、app.js

import React, { Component } from 'react'
import Count from './container/Count'
import store from './redux/store'

export default class App extends Component {

   render () {
    return (
      <div>
        {/*给容器组件传递store*/}
        <Count store={store}/>
      </div>
    )
  }
}

在这里插入图片描述

3、provider组件的使用

提供渲染,不需要单独再强制渲染app组件
项目下index.js

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

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);


4、整合ui组件和容器组件

在这里插入图片描述

5、redux开发者工具

还需要安装redux-devtools-extension

npm install --save-dev redux-devtools-extension

在sotre.js中引入

import {composWithDevTools} from 'redux-devtools-extension'

后面还有一些繁琐的内容

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
react-native-redux-router是一个用于在React Native应用中管理路由和状态的库。它结合了React Native、ReduxReact Navigation,提供了一种方便的方式来处理应用程序的导航和状态管理。 下面是一个简单的示例,演示了如何在React Native应用中使用react-native-redux-router: 1. 首先,安装所需的依赖项。在终端中运行以下命令: ```shell npm install react-native react-redux redux react-navigation react-native-router-flux --save ``` 2. 创建一个Redux store,并将其与React Native应用程序的根组件连接起来。在App.js文件中,添加以下代码: ```javascript import React from 'react'; import { Provider } from 'react-redux'; import { createStore } from 'redux'; import rootReducer from './reducers'; import AppNavigator from './navigation/AppNavigator'; const store = createStore(rootReducer); export default function App() { return ( <Provider store={store}> <AppNavigator /> </Provider> ); } ``` 3. 创建一个导航器组件,并定义应用程序的导航结构。在navigation/AppNavigator.js文件中,添加以下代码: ```javascript import { createAppContainer } from 'react-navigation'; import { createStackNavigator } from 'react-navigation-stack'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import { Actions } from 'react-native-router-flux'; import HomeScreen from '../screens/HomeScreen'; import DetailsScreen from '../screens/DetailsScreen'; const MainNavigator = createStackNavigator({ Home: { screen: HomeScreen }, Details: { screen: DetailsScreen }, }); const AppNavigator = createAppContainer(MainNavigator); const mapStateToProps = state => ({ // 将Redux状态映射到导航器组件的props中 }); const mapDispatchToProps = dispatch => bindActionCreators(Actions, dispatch); export default connect(mapStateToProps, mapDispatchToProps)(AppNavigator); ``` 4. 创建屏幕组件,并在其中使用导航和Redux状态。在screens/HomeScreen.js文件中,添加以下代码: ```javascript import React from 'react'; import { View, Text, Button } from 'react-native'; import { Actions } from 'react-native-router-flux'; const HomeScreen = () => { return ( <View> <Text>Welcome to the Home Screen!</Text> <Button title="Go to Details" onPress={() => Actions.details()} /> </View> ); } export default HomeScreen; ``` 5. 创建其他屏幕组件,并在其中使用导航和Redux状态。在screens/DetailsScreen.js文件中,添加以下代码: ```javascript import React from 'react'; import { View, Text, Button } from 'react-native'; import { Actions } from 'react-native-router-flux'; const DetailsScreen = () => { return ( <View> <Text>Welcome to the Details Screen!</Text> <Button title="Go back" onPress={() => Actions.pop()} /> </View> ); } export default DetailsScreen; ``` 这是一个简单的示例,演示了如何在React Native应用中使用react-native-redux-router来管理路由和状态。你可以根据自己的需求进行扩展和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值