Skip to content
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

Getting at the wrapped React component when exporting withStyles #10106

Closed
snydersaurus opened this issue Jan 31, 2018 · 17 comments
Closed

Getting at the wrapped React component when exporting withStyles #10106

snydersaurus opened this issue Jan 31, 2018 · 17 comments
Labels
support: question Community support but can be turned into an improvement

Comments

@snydersaurus
Copy link

snydersaurus commented Jan 31, 2018

Is it possible to get at the React class wrapped by WithStyles?

Context: getting the ref to a child component e.g.

<MyComponent ref="myComponent"></MyComponent>

Trying to reference this.refs.myComponent gives me a WithStyles object obviously and not a MyComponent object.

@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Jan 31, 2018
@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 31, 2018

@snydersaurus withStyles enhance the components with an innerRef property. You can use it.

<MyComponent innerRef={node => this.myComponent = node} />

Edit for v4: this workaround is no longer needed. You can simply provide a ref now.

@snydersaurus
Copy link
Author

Thank you @oliviertassinari. I saw that innerRef mention in the docs but it wasn't clear how to use it.

@rolandjitsu
Copy link
Contributor

@oliviertassinari I assume that the innerRef prop. does not support react's new createRef() object instead of a fn?

@oliviertassinari
Copy link
Member

@rolandjitsu Why wouldn't it work? It should.

@rolandjitsu
Copy link
Contributor

Because if I follow the example illustrated at createref-api and replace ref with innerRef, I get the following error:

index.js:2178 Warning: Failed prop type: Invalid prop `innerRef` of type `object` supplied to `WithStyles(DataPortal)`, expected `function`.
    in WithStyles(DataPortal) (created by Connect(WithStyles(DataPortal)))
    in Connect(WithStyles(DataPortal)) (created by App)
    in div (created by App)
    in App (created by WithStyles(App))
    in WithStyles(App) (created by Connect(WithStyles(App)))
    in Connect(WithStyles(App))
    in MuiThemeProvider
    in Provider

@oliviertassinari
Copy link
Member

oliviertassinari commented May 9, 2018

@rolandjitsu Aside from the PropType we need to fix. Does the feature work?

@rolandjitsu
Copy link
Contributor

@oliviertassinari yes it does 😄

@oliviertassinari
Copy link
Member

oliviertassinari commented May 9, 2018

@rolandjitsu Do you want to update the propTypes? :)

@rolandjitsu
Copy link
Contributor

sure thing 😄

@DarkKnight1992
Copy link
Contributor

damn it i spend 2 hrs trying to figure out what the problem... I think this should used as disclaimer instead ;). Love the work you guys are putting in

@oliviertassinari
Copy link
Member

@DarkKnight1992 We have started using the forwardRef capability of React. We should be able to make this behavior native and remove the innerRef property in the future.

@dmitrydwhite
Copy link

Is there an example somewhere of how the forwardRef capability of React can be used with the MUI components, e.g. Typography or Grid item?

@eps1lon
Copy link
Member

eps1lon commented Jan 17, 2019

There is currently no ref forwarding happening. We have started the effort for some components with #13722 but some dependencies are currently blocking us.

@mbrucher
Copy link

mbrucher commented Apr 12, 2019

I tried using:

innerRef={node => this.usernameRef = node}

But I got the following error:

Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()

I removed the createRef() usage, as it is not yet usable (any ETA?), but even the old pattern doesn't seem to work.

The goal was to get some values from a function triggered by a . Is tehre another pattern that I'm supposed to follow instead of using refs to get these values (and then use them in a backend request)?

@mbrucher
Copy link

@rolandjitsu Why wouldn't it work? It should.

Hi Roland, seems like there is still something missing, see my previous comment.

@joshwooding
Copy link
Member

@mbrucher can you create a new issue with a reproduction please :)

@quoctoank4it
Copy link

I tried using:

innerRef={node => this.usernameRef = node}

But I got the following error:

Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()

I removed the createRef() usage, as it is not yet usable (any ETA?), but even the old pattern doesn't seem to work.

The goal was to get some values from a function triggered by a . Is tehre another pattern that I'm supposed to follow instead of using refs to get these values (and then use them in a backend request)?

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

9 participants