Skip to content

Instantly share code, notes, and snippets.

@rkumar1904
Last active Nov 6, 2020
Embed
What would you like to do?
import React, { useEffect } from 'react';
import { connect } from 'umi';
const BasicLayout = (props) => {
const { dispatch } = props;
useEffect(() => {
dispatch({
type: 'user/getCurrentUser',
payload: {
...
...
},
})
}
}, []);
return (
<>
<ProLayout>
...
<ProLayout>
</>
);
};
// to connect the redux store
export default connect(({ user }) => ({
user
}))(BasicLayout);
import { queryUsers } from '@/services/userService';
const UserModel = {
namespace: 'user', // identification in redux store
state: {
currentUser: {},
},
effects: {
// generator function
*getCurrentUser({ payload }, { call, put }) {
const response = yield call(queryUsers);
yield put({
type: 'save',
payload: response,
});
},
},
reducers: {
save(state, { payload }) {
return {
...state,
...payload
};
},
clear() {
return {};
},
},
};
export default UserModel;
import { apiAccess, apiSecret, url } from '@/utils/env';
import request from 'umi-request';
const headers = {
'Content-Type': 'application/json',
'api-access': apiAccess,
'api-secret': apiSecret,
};
export async function queryUsers(params) {
return request(`${url}/getCurrentUser`, {
headers,
method: 'POST',
data: params,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment