Skip to content

Commit 79d953d

Browse files
committed
Add getConstant to hook
1 parent 3cfd875 commit 79d953d

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

USER_GUIDE.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,21 @@ You can change the separator used in the styles' definitions.
243243

244244
```js
245245
import { getConstant } from 'react-native-use-styles';
246+
import useStyles from from 'namespaced-styles';
246247

247-
// global
248-
const myConstant = getConstant('redColor');
248+
// namespaced
249+
const myConstant = useStyles.getConstant('redColor');
249250

250251
// explicit namespace
251252
const myConstant = getConstant('redColor', 'mynamespace');
252253

253254
// implicit namespace
254255
const myConstant = getConstant('redColor', useStyles);
255-
256256
// or
257257
const myConstant = getConstant('redColor', useStyles.namespace);
258+
259+
// global
260+
const myConstant = getConstant('redColor');
258261
```
259262

260263
Sometimes you may want to reuse a constant outside of styles. In that case, you can use getConstant.

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type TagStyles = (strings: string[], expressions: string[]) => StyleObject;
1212
type UseStyles = {
1313
(): (dependencies?: Array<any>) => TagStyles;
1414
namespace: UniqueSpace;
15+
getConstant: (name: string) => any;
1516
};
1617

1718
export function useGlobalStyles(

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-use-styles",
3-
"version": "1.3.6",
3+
"version": "1.3.7",
44
"description": "React Native useStyles",
55
"main": "index.js",
66
"module": "src/index.js",

src/core/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useRef } from 'react';
22
import { hasComputed, getPathFromLiteralTag } from '../utils';
33
import { GlobalUse, GlobalStyles } from './manager';
4+
import { getConstant } from './cache';
45

56
const recomputeMutation = (cache, dependencies) => {
67
for (let [key, { compute }] of Object.entries(cache)) {
@@ -55,6 +56,7 @@ export const Styles = (definition, namespace) => {
5556
const useStyles = (dependencies) =>
5657
useGlobalStyles(definitionNamespace, dependencies);
5758
useStyles.namespace = definitionNamespace;
59+
useStyles.getConstant = (name) => getConstant(name, definitionNamespace);
5860

5961
return useStyles;
6062
};

tests/core/index.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ describe('utils', () => {
9797
});
9898
});
9999

100+
it('useStyles getConstant returns the namespaced constant', () => {
101+
const useStyles = Styles({
102+
constants: {
103+
title: 40,
104+
},
105+
});
106+
107+
expect(useStyles.getConstant('title')).toBe(40);
108+
});
109+
100110
it('useGlobalStyles hook returns a style', () => {
101111
const useStyles = Styles({
102112
style: 'color:blue',
@@ -171,7 +181,7 @@ describe('utils', () => {
171181
styles={s`&style`}
172182
title="changeState"
173183
testID="changeState"
174-
onPress={() => setIsFirst(current => !current)}
184+
onPress={() => setIsFirst((current) => !current)}
175185
/>
176186
);
177187
};

0 commit comments

Comments
 (0)