Skip to content

Optionally disable wrappedInstance ref #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ or as an [ES7 class decorator](https://github.com/wycats/javascript-decorators)
- `options.className` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)=** Control the class name set on the wrapper `<div>`
- `options.elementResize` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)=** Set true to watch the wrapper `div` for changes in
size which are not a result of window resizing - e.g. changes to the flexbox and other layout. (optional, default `false`)
- `options.withRef` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)=** Set true to enable accessing the wrapped instance with
[getWrappedInstance()](https://github.com/digidem/react-dimensions#getwrappedinstance). Disable this when wrapping stateless components, as they cannot use `refs`. (optional, default `true`)

**Examples**

Expand Down
2 changes: 1 addition & 1 deletion example.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MyComponent extends React.Component {
}}>
{`${this.props.containerWidth}px x ${this.props.containerHeight}px`}
</div>
)
)
}
}

Expand Down
8 changes: 6 additions & 2 deletions index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ module.exports = function Dimensions ({
getDimensions = defaultGetDimensions,
debounce = 0,
debounceOpts = {},
elementResize = false
elementResize = false,
withRef = true
} = {}) {
return (ComposedComponent) => {
return class DimensionsHOC extends React.Component {
Expand Down Expand Up @@ -152,6 +153,9 @@ module.exports = function Dimensions ({
* @return {object} The rendered React component
**/
getWrappedInstance () {
if (!withRef) {
console.warn('You need to enable the `withRef` option to access the wrapped instance')
}
return this.refs.wrappedInstance
}

Expand All @@ -173,7 +177,7 @@ module.exports = function Dimensions ({
{...this.state}
{...this.props}
updateDimensions={this.updateDimensions}
ref='wrappedInstance'
ref={withRef && 'wrappedInstance'}
/>
}
</div>
Expand Down