Skip to content

Commit 175429e

Browse files
committed
feat: createRef() simplified and demo
1 parent b4ca13d commit 175429e

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Component, createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {action} from '@storybook/addon-actions';
4+
import {linkTo} from '@storybook/addon-links';
5+
import {createRef} from '../createRef';
6+
import ShowDocs from '../../../.storybook/ShowDocs'
7+
8+
class Example extends Component<any, any> {
9+
divRef = createRef();
10+
11+
onClick = () => {
12+
console.log('ref', this.divRef.value);
13+
};
14+
15+
render () {
16+
return <div ref={this.divRef} onClick={this.onClick}>foobar</div>;
17+
}
18+
}
19+
20+
storiesOf('Shims/createRef()', module)
21+
.add('Basic example', () => <Example />);

src/shim/createRef.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
const shimCreateRef = (React) => {
2-
if (typeof React.createRef !== 'function') {
3-
React.createRef = () => {
4-
const ref: any = (el) => {
5-
ref.value = el;
6-
};
1+
export const createRef = () => {
2+
const ref: any = (el) => {
3+
ref.value = el;
4+
};
75

8-
return ref;
9-
};
10-
}
6+
return ref;
117
};
12-
13-
export default shimCreateRef;

0 commit comments

Comments
 (0)