本文整理匯總了TypeScript中src/store.getState函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript getState函數的具體用法?TypeScript getState怎麽用?TypeScript getState使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了getState函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: switch
const _getEntityByIDFromStore = (
entityType: EntityType,
entityID: string | number
) => {
if (!entityType || !entityID) {
return;
}
const _store = store.getState();
const {
linodes,
domains,
nodeBalancers,
images,
volumes
} = _store.__resources;
switch (entityType) {
case 'linode':
return linodes.entities.find(linode => entityID === linode.id);
case 'image':
return images.entities.find(image => entityID === image.id);
case 'nodebalancer':
return nodeBalancers.itemsById[entityID];
case 'domain':
return domains.entities.find(domain => entityID === domain.id);
case 'volume':
return volumes.itemsById[entityID];
default:
return;
}
};
示例2: path
Axios.interceptors.request.use((config: AxiosRequestConfig): AxiosRequestConfig => {
const state = store.getState();
const token = path(['authentication', 'token'], state);
return {
...config,
headers: {
...config.headers,
...(token && { Authorization: `Bearer ${token}` }),
},
};
});
示例3: pathOr
(config: AxiosRequestConfig): AxiosRequestConfig => {
const state = store.getState();
/** Will end up being "Admin: 1234" or "Bearer 1234" */
const token =
ACCESS_TOKEN || pathOr('', ['authentication', 'token'], state);
return {
...config,
headers: {
...config.headers,
...(token && { Authorization: `${token}` })
}
};
}
示例4: it
it('should set tokens when setToken is invoked', () => {
store.dispatch(
handleStartSession({
token: 'helloworld',
scopes: '*',
expires: 'never'
})
);
expect(store.getState().authentication).toEqual({
token: 'helloworld',
scopes: '*',
expiration: 'never',
loggedInAsCustomer: false
});
});
示例5: pathOr
export const formatDate = (
date: string,
options: FormatDateOptions = {}
): string => {
let time;
/** get the timezone from redux and use it as the moment timezone */
const reduxProfile = store.getState().__resources.profile;
const userTimezone = pathOr('GMT', ['data', 'timezone'], reduxProfile);
try {
// Unknown error was causing this to crash in rare situations.
time = moment.utc(date).tz(userTimezone);
} catch (e) {
// Better to return a blank date than an error or incorrect information.
reportException(e);
return 'Error getting datetime';
}
const formattedTime = shouldHumanize(time, options.humanizeCutoff)
? time.fromNow()
: time.format(options.format || ISO_FORMAT);
return formattedTime;
};