[英]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.