简体   繁体   English

如何使用 React 中的 class 组件从 Redux 商店获取数据

[英]How to get the data from Redux store using class components in React

I am working on a React project, In my project I am trying to implement redux to store data and我正在开发一个 React 项目,在我的项目中,我正在尝试实现 redux 来存储数据和

Everything.一切。 In my project One form is there when the user fills the form and clicks the submit在我的项目中,当用户填写表单并单击提交时,存在一个表单

Button, then the Information is stored in redux store.按钮,则信息存储在 redux 存储中。 Now I am trying to get the stored data现在我正在尝试获取存储的数据

In another component.在另一个组件中。 But I am unable to get the data from the store it is showing empty array.但是我无法从它显示为空数组的商店中获取数据。

This is Getusers.js这是 Getusers.js

import React, { Component } from 'react';
import './Getusers.css';
import { Table } from 'reactstrap';
import store from '../../../Components/Redux/Store/store';


class Getusers extends Component {
    constructor(props) {
        super(props)

        this.state = {
            list: []
        }

        store.subscribe(() => {
            this.setState({
                list: store.getState().userList
            })
        })

        console.warn(this.state.list)
    }



    render() {
        return (
            <Table striped>
                <thead>
                    <tr>
                        <th>So No</th>
                        <th>Name</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </Table>
        )
    }
}

export default Getusers

    import React, { Component } from 'react';
    import './Getusers.css';
    import { Table } from 'reactstrap';
    import store from '../../../Components/Redux/Store/store';
    import {connect} from 'react-redux';

    class Getusers extends Component {
        constructor(props) {
            super(props)

            this.state = {
                list: []
            }

            store.subscribe(() => {
                this.setState({
                    list: this.props.list
                })
            })

            console.warn(this.state.list)
        }



        render() {
            return (
                <Table striped>
                    <thead>
                        <tr>
                            <th>So No</th>
                            <th>Name</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </Table>
            )
        }
    }

    const mapStateToProps = state => {
//replace Reducer name with state.'Your Reducer name' and .property
      return {
        list: state.getState.userList,
      };
    };
    const mapDispatchToProps = dispatch => {
      return {
        CallinComponent: () => {
          dispatch(MiddlewareName.ActionName());
        },
    };

    export default connect(mapStateToProps, mapDispatchToProps)(Getusers);
import React, { Component } from 'react'; import './Getusers.css'; import { Table } from 'reactstrap'; import store from '../../../Components/Redux/Store/store'; class Getusers extends Component { constructor(props) { super(props) this.state = { list: [] } store.subscribe(() => { this.setState({ list: store.getState().userList }) }) console.warn(this.state.list) } render() { const userData = this.props.propName return ( <Table striped> <thead> <tr> <th>So No</th> <th>Name</th> <th>Actions</th> </tr> </thead> <tbody> </tbody> </Table> ) } } let mapStateToProps = (state) => { return { propName: state.user, }; }; let mapDispatchToProps = (dispatch) => { return { dispatchName: (data) => dispatch(reducer(data)), }; }; const GetusersComponent = connect( mapStateToProps, mapDispatchToProps )(Getusers); export default GetusersComponent;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 store/react redux 获取数据 - How to get data from store / react redux 如何使用react-redux访问组件中的存储数据 - How to access store data in components using react-redux 在反应组件中,一旦使用 redux 从另一个组件更改状态,如何从状态中获取最新数据? - In a react components, how to get latest data from state once it is changed from another component using redux? 如何在 React-redux 渲染之前从商店获取数据? - How to get data from store before render in React-redux? 如何从 redux 存储中获取数据并在 React Native 的组件中使用它 - How to get data from the redux store and use it in component in React Native 反应:如何从 redux 存储在渲染组件中的数据? - react: how to get data from redux store in a rendered component? 如何使用 Z1E35BCBA2DBA10897C7BD0805D451 在 CLASS 反应组件中获取 redux 存储或 redux 调度 - How to get redux store or redux dispatch in CLASS react component using typescript 如何从 Redux 商店更新 React 中较低组件中的道具? - How to update props in lower components in React from Redux store? 如何从React组件react-redux中的异步函数获取数据? - How to get data from async functions in react components, react-redux? 如何使用功能组件从 Redux 存储中检索数据并避免 useSelector 和 useDispatch - How to retrieve data from Redux store using functional components and avoiding useSelector & useDispatch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM