reactjs - 如何使用 react-redux 显示所有用户的列表?
问题描述
我一直在尝试显示用户列表,但我没有找到错误的地方。有人可以帮帮我吗?如何显示用户列表。我已经附加了我从后端获得的响应。
这是代码:
用户列表.js
import React, { Component } from 'react'
import {connect} from 'react-redux'
import {listAllUsers} from '../../actions'
class ListUsers extends Component {
componentDidMount(){
this.props.listAllUsers()
}
usersList(){
return this.props.users.map((user)=>{
return (
<div key={user.id}>
<div>{user.fullname}</div>
<div>{user.address}</div>
<div>
<button>Edit</button>
</div>
<div>
<button>Delete</button>
</div>
</div>
)
})
}
render() {
return (
<div>
<h1>Users List</h1>
{this.usersList()}
</div>
)
}
}
const mapStateToProps=(state)=>{
console.log(state)
return {users:state.users}
}
export default connect(mapStateToProps,{listAllUsers:listAllUsers})(ListUsers)
动作.js
import {ADD_USER,USERS_LIST} from '../actions/types'
import users from '../apis/users'
import { v4 as uuid } from 'uuid'
export const listAllUsers=()=>{
return async (dispatch)=>{
const response=await users.get('/users')
dispatch({
type:USERS_LIST,
payload:response.data
})
}
}
userReducer.js
import {USERS_LIST} from '../actions/types'
const userReducer=(state=[],action)=>{
switch(action.type){
case USERS_LIST:
return [...state,action.payload]
default:
return state
}
}
export default userReducer
解决方案
推荐阅读
- html - VBA网页抓取:点击可点击的下拉菜单(不悬停)
- java - Drools:比较来自同一对象的 2 个数组列表
- google-apps-script - 粘贴值以用作公式(Google 表格的应用程序脚本)
- google-app-maker - 保存谷歌应用程序制造商发送的电子邮件的网址
- mysql - SQLSTATE[22023]:无效参数值:3037 提供给函数 mbrcontains 的 GIS 数据无效
- python - 总线错误:两个矩阵相乘时为 10
- woocommerce - woocommerce 交易电子邮件:使用 g 套件或 mailgun?建议和经验
- xamarin.forms - 如何以xamarin形式找到两点之间的行驶距离
- javascript - HTML 游戏中的 JS 无法运行
- python - Python 返回单行代码的行为与多行代码不同