Skip to content

Commit

Permalink
[withWidth] Document the render prop (#13074)
Browse files Browse the repository at this point in the history
* 📝 render props doc for withWidth

* well done
  • Loading branch information
JulienUsson authored and oliviertassinari committed Oct 10, 2018
1 parent d458811 commit a34a2a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/src/pages/layout/breakpoints/RenderPropsWithWidth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import withWidth from '@material-ui/core/withWidth';
import Typography from '@material-ui/core/Typography';
import toRenderProps from 'recompose/toRenderProps';

const WithWidth = toRenderProps(withWidth());

function RenderPropsWithWidth() {
return (
<WithWidth>
{({ width }) => <Typography variant="subtitle1">Current width: {width}</Typography>}
</WithWidth>
);
}

export default RenderPropsWithWidth;
7 changes: 7 additions & 0 deletions docs/src/pages/layout/breakpoints/breakpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ In the following demo, we change the rendered DOM element (*em*, <u>u</u>, ~~del

⚠️ `withWidth()` server-side rendering support is limited.

### Render Props

In some cases, you could have property name collisions using higher-order components.
To avoid the issue, you can use the [render props](https://reactjs.org/docs/render-props.html) pattern like in the following demo.

{{"demo": "pages/layout/breakpoints/RenderPropsWithWidth.js"}}

## API

### `withWidth([options]) => higher-order component`
Expand Down
7 changes: 7 additions & 0 deletions pages/layout/breakpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ module.exports = require('fs')
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/layout/breakpoints/WithWidth'), 'utf8')
`,
},
'pages/layout/breakpoints/RenderPropsWithWidth.js': {
js: require('docs/src/pages/layout/breakpoints/RenderPropsWithWidth').default,
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/layout/breakpoints/RenderPropsWithWidth'), 'utf8')
`,
},
}}
Expand Down

0 comments on commit a34a2a5

Please sign in to comment.