Daniela Matos de Carvalho
Software Engineer @Dashlane, mother, photographer amateur, former @requirelx organiser, prev @YLDio, @zpx_interactive
References
References to Components
References inside Components
var PartyComponent = React.render(<PartyComponent />, myContainer);
References to Components
React.render returns a reference to the mounted component
References to Components
JSX doesn't return a component instance, but a ReactElement
- primary type in React
- created with React.createElement()
Remember that...
References to Components
var partyElement = <PartyComponent />; // This is just a ReactElement.
var partyComponentInstance = React.render(partyElement, myContainer);
Should only be used at top level
References inside Components
use references via refs
References inside Components
Use case
focus an input after update value
References inside Components
Use case
focus an input after update value
<input value={this.state.vodooInput} />
References inside Components
Use case
focus an input after update value
<input value={this.state.vodooInput} onChange={this.handleChange.bind(this)} />
References inside Components
Use case
focus an input after update value
class PartyComponent extends React.Component {
constructor(){
super();
this.state = { vodooInput: '' };
}
handleChange(e) {
this.setState({ vodooInput: this.state.vodooInput });
}
clearAndFocusInput() {
// TODO
}
render() {
return (
<div>
<div onClick={this.clearAndFocusInput.bind(this)}>Reset and Focus</div>
<input value={this.state.vodooInput} onChange={this.handleChange.bind(this)} />
</div>
);
}
};
References inside Components
Use case
focus an input after update value
class PartyComponent extends React.Component {
/* ... */
clearAndFocusInput() {
// clear
this.setState({vodooInput: ''});
// TODO focus
},
render() {
return (
<div>
<div onClick={this.clearAndFocusInput.bind(this)}>Reset and Focus</div>
<input value={this.state.vodooInput} onChange={this.handleChange.bind(this)} />
</div>
);
}
};
References inside Components
How can we find the input node?
References inside Components
How can we find the input node?
Anti-pattern: create a variable to the input element
render() {
let vodooInput = <input />;
this.rememberThisInput = vodooInput;
return (
<div>
{vodooInput}
</div>
);
}
This won't work!
References inside Components
Solution: ref String attribute
<input value={this.state.vodooInput} ref="vodoo" onChange={this.handleChange.bind(this)} />
accessed by this.refs.vodoo
also works with non-DOM elements (react components)
References inside Components
Use case
focus an input after update value
//focus
React.findDOMNode(this.refs.vodoo).focus();
React.findDOMNode(component)
call to get a reference to the component's DOM node
References inside Components
//focus
React.findDOMNode(this.refs.vodoo).focus();
React.findDOMNode(component)
call to get a reference to the component's DOM node
Notes
React.getDOMNode(component) - deprecated
References inside Components
class PartyComponent extends React.Component {
/* ... */
clearAndFocusInput() {
// clear
this.setState({vodooInput: ''});
//focus
React.findDOMNode(this.refs.vodoo).focus();
}
render() {
return (
<div>
<div onClick={this.clearAndFocusInput.bind(this)}>Reset and Focus</div>
<input value={this.state.vodooInput} onChange={this.handleChange.bind(this)} />
</div>
);
}
};
References inside Components
Notes
- refs must be used in cases that are inherently non REACTive
- we must use mainly props and state to our data flow
References inside Components
Benefits
- access DOM node
React.findDOMNode(this.refs.myInput)
- refs are managed by the programmer
References inside Components
Cautions
- Do not access refs inside render()
- never access as a property what was specified as a string
ref="myInput" this.refs["myInput"]
- think between using state and refs
DEMO
Let's try this!
{ /* Try References */ }
1. Exercice
#13-references
By Daniela Matos de Carvalho
Software Engineer @Dashlane, mother, photographer amateur, former @requirelx organiser, prev @YLDio, @zpx_interactive