"ref" callback and "componentDidMount" not being triggered on initial render
See original GitHub issueDescription
ref
callback and componentDidMount
life-cycle hook is not being triggered until after manually triggering an additional re-render with setState
.
Also asked on SO, but not getting any responses: http://stackoverflow.com/questions/43009993/react-native-initial-lifecycle-hooks-not-being-called/43010267#43010267
Reproduction Steps and Sample Code
“constructor”, “componentWillMount”, and “render” are called immediately upon launching the application, as expected. However, “ref”, and “componentDidMount” are not called until manually triggering a re-render via this.setState() after the 5 second delay.
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
class TestText extends Component {
constructor(props, context) {
super(props, context);
console.warn('constructor');
this.textRef = null;
}
componentWillMount() {
console.warn('willMount');
}
componentDidMount() {
console.warn('didMount');
}
render() {
console.warn('render');
return (
<View>
<Text
ref={(me) => {
console.warn('ref: ' + (me ? 'not null' : 'null'));
this.textRef = me;
}}>
{'TEST'}
</Text>
</View>
);
}
}
export default class AwesomeProject extends Component {
constructor(props, context) {
super(props, context);
this.state = {
test: 0
};
setTimeout(() => {
this.setState({
test: 1
});
}, 5000)
}
render() {
return (
<View style={styles.container}>
<TestText />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
Solution
Expected behavior: Shouldn’t “ref” and “componentDidMount” be called immediately after rendering. I shouldn’t have to setState
to get those hooks to run.
Additional Information
- React Native version: 0.42.3
- Platform: Android (maybe iOS as well)
- Development Operating System: Windows
- Dev tools: compileSdkVersion = 23, buildToolsVersion = 23.0.1
Issue Analytics
- State:
- Created 6 years ago
- Reactions:16
- Comments:7 (1 by maintainers)
Top Results From Across the Web
componentDidMount called BEFORE ref callback
React will only call ref callbacks for elements that you actually returned from render. ... and initially this.state.isLoading is true , you ...
Read more >Refs and the DOM - React
React will call the ref callback with the DOM element when the component mounts, and call it with null when it unmounts. Refs...
Read more >React Callback Refs — a Complex Case | by E.Y. - Medium
This is because ref is first set after the first render() , but before componentDidMount() . So when you first render, there's no...
Read more >Rendering and Updating Data using Component Lifecycle ...
The componentDidMount() method will be triggered as soon as the component is mounted or inserted into the DOM. The basic syntax to use ......
Read more >React Lifecycle - W3Schools
The componentDidMount() method is called after the component is rendered. This is where you run statements that requires that the component is already...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sourcesoft Yes, found the solution. I am using Native Base and in my case the culprit was
floatingLabel
on<Item floatingLabel last>
component. If I removefloatingLabel
,ref
works fine, but withfloatingLabel
I need to usegetRef
as explained here.Here is the code that is working for me. Note the
getRef
in second input field.Even I am facing this issue.
Here is the test code:
ref
callback is not getting called on rendering ofInpput
component. Ideally we should see below console logsBut on console I am only seeing
Also seeing below warning / error message from React-Native (as I am calling
this.refs.detailsField.focus()
incomponentDidMount()
Looks like
ref
callback onInput
field is not getting called at all. Is my understanding wrong aboutref
or is it a bug ?Please try this test case.