Skip to content

Commit e30b73a

Browse files
authored
Merge pull request #80 from lzcabrera/master
(docs): add a 'defaultMatches prop for SSR' section on README.md
2 parents 678f78c + 9319fc7 commit e30b73a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ If you're curious about how react-media differs from [react-responsive](https://
119119

120120
Enjoy!
121121

122+
### `defaultMatches` prop for server-side rendering
123+
124+
This component comes with a `defaultMatches` prop and its default is set to true.
125+
126+
When rendering on the server you can use the `defaultMatches` prop to set the initial state on the server to match whatever you think it will be on the client. You can detect the user's device by analyzing the user-agent string from the HTTP request in your server-side rendering code.
127+
128+
```
129+
initialState = {
130+
device: 'mobile' // add your own guessing logic here
131+
};
132+
133+
<div>
134+
<Media query=“(max-width: 500px)" defaultMatches={state.device === 'mobile'} render={() => (
135+
<Text>Render me below medium breakpoint.</Text>
136+
)}/>
137+
138+
<Media query=“(min-width: 501px)" defaultMatches={state.device === 'desktop'} render={() => (
139+
<Text>Render me above medium breakpoint.</Text>
140+
)}/>
141+
</div>
142+
```
143+
122144
## About
123145

124146
`react-media` is developed and maintained by [React Training](https://reacttraining.com). If you're interested in learning more about what React can do for your company, please [get in touch](mailto:[email protected])!

0 commit comments

Comments
 (0)