Skip to content

Commit d3dad44

Browse files
authored
Use new React context (#624)
1 parent 35996f5 commit d3dad44

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

src/SortableContainer/index.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import PropTypes from 'prop-types';
32
import {findDOMNode} from 'react-dom';
43
import invariant from 'invariant';
54

@@ -37,17 +36,24 @@ import {
3736
defaultKeyCodes,
3837
} from './props';
3938

39+
export const SortableContext = React.createContext({
40+
manager: {},
41+
});
42+
4043
export default function sortableContainer(
4144
WrappedComponent,
4245
config = {withRef: false},
4346
) {
4447
return class WithSortableContainer extends React.Component {
4548
constructor(props) {
4649
super(props);
50+
const manager = new Manager();
4751

4852
validateProps(props);
4953

50-
this.manager = new Manager();
54+
this.manager = manager;
55+
this.wrappedInstance = React.createRef();
56+
this.sortableContextValue = {manager};
5157
this.events = {
5258
end: this.handleEnd,
5359
move: this.handleMove,
@@ -60,15 +66,6 @@ export default function sortableContainer(
6066
static displayName = provideDisplayName('sortableList', WrappedComponent);
6167
static defaultProps = defaultProps;
6268
static propTypes = propTypes;
63-
static childContextTypes = {
64-
manager: PropTypes.object.isRequired,
65-
};
66-
67-
getChildContext() {
68-
return {
69-
manager: this.manager,
70-
};
71-
}
7269

7370
componentDidMount() {
7471
const {useWindowAsScrollContainer} = this.props;
@@ -898,7 +895,7 @@ export default function sortableContainer(
898895
'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableContainer() call',
899896
);
900897

901-
return this.refs.wrappedInstance;
898+
return this.wrappedInstance.current;
902899
}
903900

904901
getContainer() {
@@ -1047,9 +1044,13 @@ export default function sortableContainer(
10471044
};
10481045

10491046
render() {
1050-
const ref = config.withRef ? 'wrappedInstance' : null;
1047+
const ref = config.withRef ? this.wrappedInstance : null;
10511048

1052-
return <WrappedComponent ref={ref} {...omit(this.props, omittedProps)} />;
1049+
return (
1050+
<SortableContext.Provider value={this.sortableContextValue}>
1051+
<WrappedComponent ref={ref} {...omit(this.props, omittedProps)} />
1052+
</SortableContext.Provider>
1053+
);
10531054
}
10541055

10551056
get helperContainer() {

src/SortableElement/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import PropTypes from 'prop-types';
33
import {findDOMNode} from 'react-dom';
44
import invariant from 'invariant';
5+
import {SortableContext} from '../SortableContainer';
56

67
import {provideDisplayName, omit} from '../utils';
78

@@ -23,9 +24,7 @@ export default function sortableElement(
2324
WrappedComponent,
2425
);
2526

26-
static contextTypes = {
27-
manager: PropTypes.object.isRequired,
28-
};
27+
static contextType = SortableContext;
2928

3029
static propTypes = propTypes;
3130

@@ -84,11 +83,13 @@ export default function sortableElement(
8483
config.withRef,
8584
'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableElement() call',
8685
);
87-
return this.refs.wrappedInstance;
86+
return this.wrappedInstance.current;
8887
}
8988

89+
wrappedInstance = React.createRef();
90+
9091
render() {
91-
const ref = config.withRef ? 'wrappedInstance' : null;
92+
const ref = config.withRef ? this.wrappedInstance : null;
9293

9394
return <WrappedComponent ref={ref} {...omit(this.props, omittedProps)} />;
9495
}

src/SortableHandle/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ export default function sortableHandle(
2121
config.withRef,
2222
'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableHandle() call',
2323
);
24-
return this.refs.wrappedInstance;
24+
return this.wrappedInstance.current;
2525
}
2626

27+
wrappedInstance = React.createRef();
28+
2729
render() {
28-
const ref = config.withRef ? 'wrappedInstance' : null;
30+
const ref = config.withRef ? this.wrappedInstance : null;
2931

3032
return <WrappedComponent ref={ref} {...this.props} />;
3133
}

0 commit comments

Comments
 (0)