Reactjs 如何使用React中的类组件从Redux存储中获取数据
我正在做一个React项目,在我的项目中,我试图实现redux来存储数据和数据 一切。在我的项目中,当用户填写表单并单击submit时,会出现一个表单 按钮,则信息存储在redux存储中。现在我正在尝试获取存储的数据 在另一个组件中。但我无法从显示空数组的存储中获取数据 这是Getusers.jsReactjs 如何使用React中的类组件从Redux存储中获取数据,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我正在做一个React项目,在我的项目中,我试图实现redux来存储数据和数据 一切。在我的项目中,当用户填写表单并单击submit时,会出现一个表单 按钮,则信息存储在redux存储中。现在我正在尝试获取存储的数据 在另一个组件中。但我无法从显示空数组的存储中获取数据 这是Getusers.js import React, { Component } from 'react'; import './Getusers.css'; import { Table } from 'reactstrap
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}来自'React';
导入“./Getusers.css”;
从“reactstrap”导入{Table};
从“../../../Components/Redux/store/store”导入存储;
类Getusers扩展组件{
建造师(道具){
超级(道具)
此.state={
名单:[]
}
store.subscribe(()=>{
这是我的国家({
列表:store.getState().userList
})
})
console.warn(this.state.list)
}
render(){
返回(
所以不
名称
行动
)
}
}
导出默认Getusers
从“React”导入React,{Component};
导入“./Getusers.css”;
从“reactstrap”导入{Table};
从“../../../Components/Redux/store/store”导入存储;
从'react redux'导入{connect};
类Getusers扩展组件{
建造师(道具){
超级(道具)
此.state={
名单:[]
}
store.subscribe(()=>{
这是我的国家({
列表:this.props.list
})
})
console.warn(this.state.list)
}
render(){
返回(
所以不
名称
行动
)
}
}
常量mapStateToProps=状态=>{
//将减速器名称替换为状态“您的减速器名称”和.property
返回{
列表:state.getState.userList,
};
};
const mapDispatchToProps=调度=>{
返回{
CallinComponent:()=>{
调度(MiddlewareName.ActionName());
},
};
导出默认连接(mapStateToProps、mapDispatchToProps)(Getusers);
从“React”导入React,{Component};
导入“./Getusers.css”;
从“reactstrap”导入{Table};
从“../../../Components/Redux/store/store”导入存储;
类Getusers扩展组件{
建造师(道具){
超级(道具)
此.state={
名单:[]
}
store.subscribe(()=>{
这是我的国家({
列表:store.getState().userList
})
})
console.warn(this.state.list)
}
render(){
const userData=this.props.propName
返回(
所以不
名称
行动
)
}
}
让mapStateToProps=(状态)=>{
返回{
propName:state.user,
};
};
让mapDispatchToProps=(dispatch)=>{
返回{
dispatchName:(数据)=>调度(减速机(数据)),
};
};
const GetusersComponent=connect(
MapStateTops,
mapDispatchToProps
)(用户);
导出默认GetusersComponent;
使用连接功能。您可以使用connect()
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);