Skip to content

Commit

Permalink
feat: createRef() simplified and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 7, 2018
1 parent b4ca13d commit 175429e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
21 changes: 21 additions & 0 deletions src/shim/__story__/createRef.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Component, createElement as h} from 'react';
import {storiesOf} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {linkTo} from '@storybook/addon-links';
import {createRef} from '../createRef';
import ShowDocs from '../../../.storybook/ShowDocs'

class Example extends Component<any, any> {
divRef = createRef();

onClick = () => {
console.log('ref', this.divRef.value);
};

render () {
return <div ref={this.divRef} onClick={this.onClick}>foobar</div>;
}
}

storiesOf('Shims/createRef()', module)
.add('Basic example', () => <Example />);
16 changes: 5 additions & 11 deletions src/shim/createRef.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const shimCreateRef = (React) => {
if (typeof React.createRef !== 'function') {
React.createRef = () => {
const ref: any = (el) => {
ref.value = el;
};
export const createRef = () => {
const ref: any = (el) => {
ref.value = el;
};

return ref;
};
}
return ref;
};

export default shimCreateRef;

0 comments on commit 175429e

Please sign in to comment.