Skip to content

Use nodeRef in code examples #851

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

Merged
merged 2 commits into from
Sep 18, 2022
Merged
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
5 changes: 3 additions & 2 deletions src/CSSTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ const removeClass = (node, classes) =>
* ```jsx
* function App() {
* const [inProp, setInProp] = useState(false);
* const nodeRef = useRef(null);
* return (
* <div>
* <CSSTransition in={inProp} timeout={200} classNames="my-node">
* <div>
* <CSSTransition nodeRef={nodeRef} in={inProp} timeout={200} classNames="my-node">
* <div ref={nodeRef}>
* {"I'll receive my-node-* classes"}
* </div>
* </CSSTransition>
Expand Down
6 changes: 5 additions & 1 deletion src/SwitchTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ const enterRenders = {
* ```jsx
* function App() {
* const [state, setState] = useState(false);
* const helloRef = useRef(null);
* const goodbyeRef = useRef(null);
* const nodeRef = state ? goodbyeRef : helloRef;
* return (
* <SwitchTransition>
* <CSSTransition
* key={state ? "Goodbye, world!" : "Hello, world!"}
* nodeRef={nodeRef}
* addEndListener={(node, done) => node.addEventListener("transitionend", done, false)}
* classNames='fade'
* >
* <button onClick={() => setState(state => !state)}>
* <button ref={nodeRef} onClick={() => setState(state => !state)}>
* {state ? "Goodbye, world!" : "Hello, world!"}
* </button>
* </CSSTransition>
Expand Down
43 changes: 27 additions & 16 deletions src/Transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const EXITING = 'exiting';
*
* ```jsx
* import { Transition } from 'react-transition-group';
* import { useRef } from 'react';
*
* const duration = 300;
*
Expand All @@ -52,18 +53,21 @@ export const EXITING = 'exiting';
* exited: { opacity: 0 },
* };
*
* const Fade = ({ in: inProp }) => (
* <Transition in={inProp} timeout={duration}>
* {state => (
* <div style={{
* ...defaultStyle,
* ...transitionStyles[state]
* }}>
* I'm a fade Transition!
* </div>
* )}
* </Transition>
* );
* function Fade({ in: inProp }) {
* const nodeRef = useRef(null);
* return (
* <Transition nodeRef={nodeRef} in={inProp} timeout={duration}>
* {state => (
* <div ref={nodeRef} style={{
* ...defaultStyle,
* ...transitionStyles[state]
* }}>
* I'm a fade Transition!
* </div>
* )}
* </Transition>
* );
* }
* ```
*
* There are 4 main states a Transition can be in:
Expand All @@ -80,11 +84,15 @@ export const EXITING = 'exiting';
* [useState](https://reactjs.org/docs/hooks-reference.html#usestate) hook):
*
* ```jsx
* import { Transition } from 'react-transition-group';
* import { useState, useRef } from 'react';
*
* function App() {
* const [inProp, setInProp] = useState(false);
* const nodeRef = useRef(null);
* return (
* <div>
* <Transition in={inProp} timeout={500}>
* <Transition nodeRef={nodeRef} in={inProp} timeout={500}>
* {state => (
* // ...
* )}
Expand Down Expand Up @@ -390,9 +398,12 @@ class Transition extends React.Component {

Transition.propTypes = {
/**
* A React reference to DOM element that need to transition:
* A React reference to the DOM element that needs to transition:
* https://stackoverflow.com/a/51127130/4671932
*
* - This prop is optional, but recommended in order to avoid defaulting to
* [`ReactDOM.findDOMNode`](https://reactjs.org/docs/react-dom.html#finddomnode),
* which is deprecated in `StrictMode`
* - When `nodeRef` prop is used, `node` is not passed to callback functions
* (e.g. `onEnter`) because user already has direct access to the node.
* - When changing `key` prop of `Transition` in a `TransitionGroup` a new
Expand Down Expand Up @@ -422,9 +433,9 @@ Transition.propTypes = {
* specific props to a component.
*
* ```jsx
* <Transition in={this.state.in} timeout={150}>
* <Transition nodeRef={nodeRef} in={this.state.in} timeout={150}>
* {state => (
* <MyComponent className={`fade fade-${state}`} />
* <MyComponent ref={nodeRef} className={`fade fade-${state}`} />
* )}
* </Transition>
* ```
Expand Down
1 change: 1 addition & 0 deletions www/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
3 changes: 3 additions & 0 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
},
"author": "",
"license": "MIT",
"engines": {
"node": "<=16"
},
"dependencies": {
"@babel/core": "^7.3.4",
"babel-preset-gatsby": "^2.7.0",
Expand Down