-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
ref callback argument is null #8359
Comments
The The
So if you want to use refs, you need to define |
Hi Dan, thank you for the quick response. I know that my components should start with an upper case and I read the docs before I opened this issue. My example should only illustrate the issue that I believe I misunderstood something there. I know that I can't use this syntax in stateless components like it's written in the example in the ref docs but I didn't know that I also can't use it when I consume stateless components. So is there any workaround or something else that I can do if I want the |
What example are you referring to? I think ref docs actually show an example of using it in stateless components which is supported:
As for
I think this is also what docs say explicitly:
"On", not "in". Maybe we can rephrase it to make it clearer?
Can you explain what you expect to get from a ref? With class components, you get the instance so that you could call methods on it. With functional components, there are no instances and there are no methods on them. What do you expect to get from such ref? |
Ok, I give you a more realistic example of what my current problem was. I write a React component that has different conditions what import MyComponent from './MyComponent';
import MySecondComponent from './MySecondComponent';
import ThirdPartyComponent from 'some-npm-package';
class Container extends React.Component {
componentDidMount() {
if (this.myComponent) {
// do something
} else if (this.mySecondComponent) {
// do something
} else if (this.thirdPartyComponent) {
// do something
}
}
render() {
if (foo()) {
return <MyComponent ref={comp => this.myComponent = comp} />;
}
if (bar()) {
return <MySecondComponent ref={comp => this.mySecondComponent = comp} />;
}
return <ThirdPartyComponent ref={comp => this.thirdPartyComponent = comp} />;
}
}
My current "workaround" is to set a boolean flag instead of setting the ref instance: So I don't need the |
This looks a bit like using React backwards. What do |
My first thought was like yours: I put it in the state. But this means I have to call
Yes, I think I have a thought mistake but I don't know the most elegant solution here at the moment 🤔 |
No, why? Compute them in constructor when you initialize the state. If they depend on props, also recompute them in componentWillReceiveProps. |
Ah I see! Thank you Dan. I think I got it now 😎 👍 |
Is there anything in the documentation that was missing to make it click? |
Well first it was, at least for me, not clear enough that I can't use function CustomTextInput(props) {
// textInput must be declared here so the ref callback can refer to it
let textInput = null;
function handleClick() {
textInput.focus();
}
return (
<div>
<input
type="text"
ref={(input) => { textInput = input; }} />
<input
type="button"
value="Focus the text input"
onClick={handleClick}
/>
</div>
);
} But I thought I can use Second: I think a little example for conditional rendering would be great that explains how to check if some components are rendered or not. So like my issue at the moment. I have some heavy calculation that decides if a component should rendered or not. |
As for the second part, is there something in the Conditional Rendering page that is missing? I think it includes some similar examples. |
You are right. I read it now twice times and I think it is clear enough. |
The correct behaviour would be not passing |
We are printing a warning for this in master. It will be part of React 16. |
Any update on this? I've just been bitten by this gotcha in |
Do you want to request a feature or report a bug?
A bug 🐛
What is the current behavior?
The ref callback gets called with
null
as argument instead of an instance of the component. This only occurs when you write the component name starting with an uppercase character (PascalCase). If you write the component starting with a lowercase character (camelCase) everything works as expected.This jsfiddle demonstrates how you get a
null
argument: https://jsfiddle.net/1v9vk4zb/And this demonstrates how you get a correct reference: https://jsfiddle.net/a0d0owru/
As you can see I just renamed
Button
tolowercaseButton
.What is the expected behavior?
It should always pass the component instance as argument to the callback.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
This occurs in
v15.4.0
andv15.3.1
. I didn't tested any other versions.The text was updated successfully, but these errors were encountered: