Skip to content

Instantly share code, notes, and snippets.

@rkumar1904
Last active November 6, 2020 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkumar1904/86f90952f56a89d7ecc49c1ebd819f01 to your computer and use it in GitHub Desktop.
Save rkumar1904/86f90952f56a89d7ecc49c1ebd819f01 to your computer and use it in GitHub Desktop.
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