How can a ref be added to a material-ui Box component in React, using TypeScript? This is important to allow animation libraries like GreenSock / GSAP to animate nodes. Per material-ui docs, using the itemRef will not work because it requires the use of findDOMNode which is deprecated in strict mode (prep for concurrent React) and further is likely to break due to virtual DOM rendering.
Asked
Active
Viewed 1.2k times
9

Jeffrey Benjamin Brown
- 3,427
- 2
- 28
- 40

sebastian
- 179
- 1
- 2
- 8
-
1Can you provide some coding sample that may emphasize what you are trying to do? – MEDZ Jan 18 '20 at 09:31
-
@MEDZ, thanks for the response. I have found several other posts on the Box ref limitation, posted on each, and provided a summary with a workaround, below. – sebastian Jan 18 '20 at 20:08
-
Specify setting in title – Jeffrey Benjamin Brown Jan 19 '20 at 00:20
1 Answers
3
Without the ability to associate a ref to ALL MATERIAL-UI GENERATED NODES, there is not a reliable way to integrate animation libraries targeting specific nodes.
There are several related issues on the material-ui GitHub project. I posted a workaround on Issue #17010 until such time as material-ui includes the ability to add ref to all generated nodes.
Related Issues:
- How can a ref be added to a Box component? #19284
- [Box] Allow ref on Box #19275
- ref missing from Box in TypeScript definition #17010
Interim Workaround:
// 1. Import style library, either Emotion or Styled-Components
import styled from "@emotion/styled";
// 2. Recreate the Material-UI Box with a styled component
const StyledBox = styled(Box)``;
// 3. Usage in the render
<StyledBox ref={boxRef}>Box Content</StyledBox>
NOTE: Using @material-ui/core/styles does not work, requiring the use of emotion or styled-components. We are forced to use emotion over styled-components due to an animation flicker problem uniquely generated by styled-components.

sebastian
- 179
- 1
- 2
- 8
-
1It looks like this is [fixed in material-ui v5](https://github.com/mui-org/material-ui/issues/17010#issuecomment-724187064), which is in alpha. – Enrico Borba Nov 27 '20 at 19:52