Skip to content

Commit c468c85

Browse files
committed
docs: update code examples to use nodeRef
`ReactDOM.findDOMNode()` is deprecated in StrictMode.
1 parent 5a2ff43 commit c468c85

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

src/CSSTransition.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ const removeClass = (node, classes) =>
2929
* ```jsx
3030
* function App() {
3131
* const [inProp, setInProp] = useState(false);
32+
* const nodeRef = useRef(null);
3233
* return (
3334
* <div>
34-
* <CSSTransition in={inProp} timeout={200} classNames="my-node">
35-
* <div>
35+
* <CSSTransition nodeRef={nodeRef} in={inProp} timeout={200} classNames="my-node">
36+
* <div ref={nodeRef}>
3637
* {"I'll receive my-node-* classes"}
3738
* </div>
3839
* </CSSTransition>

src/SwitchTransition.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,18 @@ const enterRenders = {
8989
* ```jsx
9090
* function App() {
9191
* const [state, setState] = useState(false);
92+
* const helloRef = useRef(null);
93+
* const goodbyeRef = useRef(null);
94+
* const nodeRef = state ? goodbyeRef : helloRef;
9295
* return (
9396
* <SwitchTransition>
9497
* <CSSTransition
9598
* key={state ? "Goodbye, world!" : "Hello, world!"}
99+
* nodeRef={nodeRef}
96100
* addEndListener={(node, done) => node.addEventListener("transitionend", done, false)}
97101
* classNames='fade'
98102
* >
99-
* <button onClick={() => setState(state => !state)}>
103+
* <button ref={nodeRef} onClick={() => setState(state => !state)}>
100104
* {state ? "Goodbye, world!" : "Hello, world!"}
101105
* </button>
102106
* </CSSTransition>

src/Transition.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const EXITING = 'exiting';
3737
*
3838
* ```jsx
3939
* import { Transition } from 'react-transition-group';
40+
* import { useRef } from 'react';
4041
*
4142
* const duration = 300;
4243
*
@@ -52,18 +53,21 @@ export const EXITING = 'exiting';
5253
* exited: { opacity: 0 },
5354
* };
5455
*
55-
* const Fade = ({ in: inProp }) => (
56-
* <Transition in={inProp} timeout={duration}>
57-
* {state => (
58-
* <div style={{
59-
* ...defaultStyle,
60-
* ...transitionStyles[state]
61-
* }}>
62-
* I'm a fade Transition!
63-
* </div>
64-
* )}
65-
* </Transition>
66-
* );
56+
* function Fade({ in: inProp }) {
57+
* const nodeRef = useRef(null);
58+
* return (
59+
* <Transition nodeRef={nodeRef} in={inProp} timeout={duration}>
60+
* {state => (
61+
* <div ref={nodeRef} style={{
62+
* ...defaultStyle,
63+
* ...transitionStyles[state]
64+
* }}>
65+
* I'm a fade Transition!
66+
* </div>
67+
* )}
68+
* </Transition>
69+
* );
70+
* }
6771
* ```
6872
*
6973
* There are 4 main states a Transition can be in:
@@ -80,11 +84,15 @@ export const EXITING = 'exiting';
8084
* [useState](https://reactjs.org/docs/hooks-reference.html#usestate) hook):
8185
*
8286
* ```jsx
87+
* import { Transition } from 'react-transition-group';
88+
* import { useState, useRef } from 'react';
89+
*
8390
* function App() {
8491
* const [inProp, setInProp] = useState(false);
92+
* const nodeRef = useRef(null);
8593
* return (
8694
* <div>
87-
* <Transition in={inProp} timeout={500}>
95+
* <Transition nodeRef={nodeRef} in={inProp} timeout={500}>
8896
* {state => (
8997
* // ...
9098
* )}
@@ -390,9 +398,12 @@ class Transition extends React.Component {
390398

391399
Transition.propTypes = {
392400
/**
393-
* A React reference to DOM element that need to transition:
401+
* A React reference to the DOM element that needs to transition:
394402
* https://stackoverflow.com/a/51127130/4671932
395403
*
404+
* - This prop is optional, but recommended in order to avoid defaulting to
405+
* [`ReactDOM.findDOMNode`](https://reactjs.org/docs/react-dom.html#finddomnode),
406+
* which is deprecated in `StrictMode`
396407
* - When `nodeRef` prop is used, `node` is not passed to callback functions
397408
* (e.g. `onEnter`) because user already has direct access to the node.
398409
* - When changing `key` prop of `Transition` in a `TransitionGroup` a new
@@ -422,9 +433,9 @@ Transition.propTypes = {
422433
* specific props to a component.
423434
*
424435
* ```jsx
425-
* <Transition in={this.state.in} timeout={150}>
436+
* <Transition nodeRef={nodeRef} in={this.state.in} timeout={150}>
426437
* {state => (
427-
* <MyComponent className={`fade fade-${state}`} />
438+
* <MyComponent ref={nodeRef} className={`fade fade-${state}`} />
428439
* )}
429440
* </Transition>
430441
* ```

0 commit comments

Comments
 (0)