我正在构建一个React组件,它基本上需要从数据库中获取数据并加载到表中
AdminActions \ index.js //我的操作
import {
SHOW_MESSAGE,
HIDE_MESSAGE,
GET_NON_ACTIVE_USERS,
GET_NON_ACTIVE_USERS_SUCCESS,
} from "constants/ActionTypes";
export const getNonActiveUsers = (options) => {
return {
type: GET_NON_ACTIVE_USERS,
payload: options
};
};
export const getNonActiveUsersSuccess = (users) => {
return {
type: GET_NON_ACTIVE_USERS_SUCCESS,
payload : users
};
};
export const showSuccessMessage = (message) => {
return {
type: SHOW_MESSAGE,
payload: message
};
};
export const hideMessage = () => {
return {
type: HIDE_MESSAGE,
};
};
AdminReducers \ index.js //我的Reducer
import {
SHOW_MESSAGE,
HIDE_MESSAGE,
GET_NON_ACTIVE_USERS_SUCCESS,
} from "constants/ActionTypes";
const INIT_STATE = {
alertMessage: '',
showMessage: false,
};
export default (state = INIT_STATE, action) => {
switch (action.type) {
case GET_NON_ACTIVE_USERS_SUCCESS:{
return {
...state,
users: action.payload,
}
}
case SHOW_MESSAGE: {
return {
...state,
alertMessage: action.payload,
showMessage: true,
loader: false
}
}
case HIDE_MESSAGE: {
return {
...state,
alertMessage: '',
showMessage: false,
}
}
default:
return state;
} }
AdminSagas \ index.js //我的传奇
import {all, call, fork, put, takeEvery} from "redux-saga/effects";
import {
GET_NON_ACTIVE_USERS,
} from "constants/ActionTypes";
import {showSuccessMessage,getNonActiveUsersSuccess } from "../../actions/AdminActions";
import{ base_url } from "../../../util/config";
const getNonActiveUsersRequest = async ({page,limit}) => {
return await fetch(base_url+"/api/admin/getNonActiveUsers",
{
method: 'get',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'Authorization' : "Bearer " + JSON.parse(localStorage.getItem('user')).token
}
})
.then(res => res.json())
.then(data => {
return data;
})
.catch(error => {
return error;
});
};
function* getNonActiveUsers(payload) {
try {
const response = yield call(getNonActiveUsersRequest,payload);
if (response.message) {
yield put(showSuccessMessage(response.message));
} else {
yield put(getNonActiveUsersSuccess(response));
}
} catch (error) {
yield put(showSuccessMessage(error));
}
};
export function* getNonActiveUsersCatcher() {
yield takeEvery(GET_NON_ACTIVE_USERS, getNonActiveUsers)
}
export default function* rootSaga() {
yield all([fork(getNonActiveUsersCatcher)
]);
}
我的组件
import React, {Component} from "react";
import {connect} from "react-redux";
import {Table} from "antd";
import {getNonActiveUsers, hideMessage} from "appRedux/actions/AdminActions/";
const columns = [{
title: 'Name & Surname',
dataIndex: 'fullName',
}, {
title: 'Email',
dataIndex: 'email',
}, {
title: 'Phone',
dataIndex: 'phone',
},{
title: 'Activation Status',
dataIndex: 'user_info.activationStatus',
}];
class ActivateInvestors extends Component {
state = {
data: [],
pagination: {},
loading: false
};
componentDidMount() {
this.props.getNonActiveUsers();
}
render() {
console.log(this.props.users);
return (
<Table
columns={columns}
rowKey={record => record._id}
dataSource={this.props.users}
pagination={this.state.pagination}
loading={this.state.loading}
onChange={this.handleTableChange}
/>
);
}
}
const mapStateToProps = ({admin}) => {
const {loader,alertMessage, showMessage, users} = admin;
return {loader,alertMessage, showMessage, users}
};
export default connect(mapStateToProps, {
getNonActiveUsers,
hideMessage,
})(ActivateInvestors);
因此第一个console.log输出未定义,我不知道如何专业地解决问题。我可以处理if支票,但我不想这样做。
我可以成功获取数据,但是我不知道在何处分配值或调用函数。
答案 0 :(得分:0)
您的第一个console.log是"undefined"
,因为到第一次执行渲染功能时,您仍在从端点获取数据。当您最终检索数据时,您会看到该组件的重新呈现,然后您获得console.log中显示的值。您需要更好地解释您想要实现的目标,以便我们更好地帮助您。
答案 1 :(得分:0)
您必须使用if进行检查,因为第一次调用渲染时未定义用户属性。减速器分配值后,它将重新渲染。到您看到日志上的数据时。
类似的东西在这里使用lodash:
if (!_.isEmpty(this.props.users)) {
return (
<Table
columns={columns}
rowKey={record => record._id}
dataSource={this.props.users}
pagination={this.state.pagination}
loading={this.state.loading}
onChange={this.handleTableChange}
/>
);
} else {
return <p>Loading...</p>;
}