@@ -27,7 +27,7 @@ But it's not limited to this case. Just use that at your needs.
27
27
#### With class component.
28
28
``` jsx
29
29
import React , { Component } from ' react' ;
30
- import Sizes from ' react-sizes' ;
30
+ import withSizes from ' react-sizes' ;
31
31
32
32
class MyComponent extends Component {
33
33
render () {
@@ -41,16 +41,16 @@ const mapSizesToProps = ({ width }) => ({
41
41
isMobile: width < 480 ,
42
42
});
43
43
44
- export Sizes (mapSizesToProps )(MyComponent );
44
+ export default withSizes (mapSizesToProps)(MyComponent);
45
45
```
46
46
You can play with this example [ here] ( https://codesandbox.io/s/Rg0DDOWnE ) .
47
47
48
48
#### As decorator.
49
49
``` jsx
50
50
import React from ' react' ;
51
- import Sizes from ' react-sizes' ;
51
+ import withSizes from ' react-sizes' ;
52
52
53
- @Sizes (({ width }) => ({ isMobile: width < 480 }))
53
+ @withSizes (({ width }) => ({ isMobile: width < 480 }))
54
54
class MyComponent extends Component {
55
55
render () {
56
56
return (
@@ -62,10 +62,33 @@ class MyComponent extends Component {
62
62
export default MyComponent ;
63
63
```
64
64
65
+ #### Interoperate with other libraries.
66
+ ``` jsx
67
+ import React from ' react' ;
68
+ import withSizes from ' react-sizes' ;
69
+ import { withState , compose } from ' recompose' ;
70
+
71
+ const enhancer = compose (
72
+ withState (' counter' , ' setCounter' , 0 ),
73
+ withSizes (({ width }) => ({ isMobile: width < 480 })),
74
+ );
75
+
76
+ const MyComponent = enhancer (({ isMobile, counter, setCounter }) => (
77
+ < div>
78
+ < div>
79
+ Count: {counter} < button onClick= {() => setCounter (n => n + 1 )}> Increment< / button>
80
+ < / div>
81
+ < div> {isMobile ? ' Is Mobile' : ' Is Not Mobile' }< / div>
82
+ < / div>
83
+ ));
84
+
85
+ export default MyComponent ;
86
+ ```
87
+
65
88
#### With functional component.
66
89
``` jsx
67
90
import React from ' react' ;
68
- import Sizes from ' react-sizes' ;
91
+ import withSizes from ' react-sizes' ;
69
92
70
93
const MyComponent = ({ isMobile }) => (
71
94
< div> {isMobile ? ' Is Mobile' : ' Is Not Mobile' }< / div>
@@ -75,30 +98,30 @@ const mapSizesToProps = ({ width }) => ({
75
98
isMobile: width < 480 ,
76
99
});
77
100
78
- export Sizes (mapSizesToProps )(MyComponent );
101
+ export default withSizes (mapSizesToProps)(MyComponent);
79
102
```
80
103
81
- #### Interoperate with other libraries.
104
+ #### Mess with props.
105
+ (Added in 0.1.0)
82
106
``` jsx
83
107
import React from ' react' ;
84
- import Sizes from ' react-sizes' ;
85
- import { withState , compose } from ' recompose' ;
108
+ import withSizes from ' react-sizes' ;
86
109
87
- const enhancer = compose (
88
- withState (' counter' , ' setCounter' , 0 ),
89
- Sizes (({ width }) => ({ isMobile: width < 480 })),
110
+ const MyComponent = ({ isMobile }) => (
111
+ < div> {isMobile ? ' Is Mobile' : ' Is Not Mobile' }< / div>
90
112
);
91
113
92
- const MyComponent = enhancer (({ isMobile, counter, setCounter }) => (
93
- < div>
94
- < div>
95
- Count: {counter} < button onClick= {() => setCounter (n => n + 1 )}> Increment< / button>
96
- < / div>
97
- < div> {isMobile ? ' Is Mobile' : ' Is Not Mobile' }< / div>
98
- < / div>
99
- ));
114
+ const mapSizesToProps = ({ width }, { mobileBreakpoint }) => ({
115
+ isMobile: width < mobileBreakpoint,
116
+ });
100
117
101
- export default MyComponent ;
118
+ export default withSizes (mapSizesToProps)(MyComponent);
119
+ ```
120
+ then:
121
+ ``` jsx
122
+ < MyComponent mobileBreakpoint= {480 } / >
123
+ < MyComponent mobileBreakpoint= {400 } / >
124
+ < MyComponent mobileBreakpoint= {600 } / >
102
125
```
103
126
104
127
#### With presets selectors.
@@ -108,26 +131,26 @@ export default MyComponent;
108
131
- });
109
132
110
133
+ const mapSizesToProps = sizes => ({
111
- + isMobile: Sizes .isMobile(sizes),
134
+ + isMobile: withSizes .isMobile(sizes),
112
135
+ });
113
136
```
114
137
115
138
## Presets Selectors
116
139
117
- You can check all ** our** presets selectors at our main code ` src/Sizes .js ` .
140
+ You can check all ** our** presets selectors at our main code ` src/withSizes .js ` .
118
141
``` js
119
- Sizes .isMobile = ({ width }) => width < 480 ;
120
- Sizes .isTablet = ({ width }) => width >= 480 && width < 1024 ;
121
- Sizes .isDesktop = ({ width }) => width >= 1024 ;
142
+ withSizes .isMobile = ({ width }) => width < 480 ;
143
+ withSizes .isTablet = ({ width }) => width >= 480 && width < 1024 ;
144
+ withSizes .isDesktop = ({ width }) => width >= 1024 ;
122
145
123
- Sizes .isGtMobile = (sizes ) => ! Sizes .isMobile (sizes);
124
- Sizes .isGtTablet = (sizes ) => Sizes .isDesktop (sizes);
146
+ withSizes .isGtMobile = (sizes ) => ! withSizes .isMobile (sizes);
147
+ withSizes .isGtTablet = (sizes ) => withSizes .isDesktop (sizes);
125
148
126
- Sizes .isStTablet = (sizes ) => Sizes .isMobile (sizes);
127
- Sizes .isStDesktop = (sizes ) => ! Sizes .isStDesktop (sizes);
149
+ withSizes .isStTablet = (sizes ) => withSizes .isMobile (sizes);
150
+ withSizes .isStDesktop = (sizes ) => ! withSizes .isStDesktop (sizes);
128
151
129
- Sizes .isTabletAndGreater = (sizes ) => ! Sizes .isMobile (sizes);
130
- Sizes .isTabletAndSmaller = (sizes ) => ! Sizes .isStDesktop (sizes);
152
+ withSizes .isTabletAndGreater = (sizes ) => ! withSizes .isMobile (sizes);
153
+ withSizes .isTabletAndSmaller = (sizes ) => ! withSizes .isStDesktop (sizes);
131
154
```
132
155
133
156
If it don't fit to your needs, you can create your own selectors.
0 commit comments