Browse Source

Merge pull request #771 from codecrutch/fix-createref-example

Move constructor out of render
main
Alex Krolick 7 years ago
committed by GitHub
parent
commit
9c9cee0b08
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      content/docs/accessibility.md

5
content/docs/accessibility.md

@ -147,13 +147,13 @@ To set focus in React, we can use [Refs to DOM elements](/docs/refs-and-the-dom.
Using this, we first create a ref to an element in the JSX of a component class:
```javascript{4-5,8-9,13}
render() {
class CustomTextInput extends React.Component {
constructor(props) {
super(props);
// Create a ref to store the textInput DOM element
this.textInput = React.createRef();
}
// ...
render() {
// Use the `ref` callback to store a reference to the text input DOM
// element in an instance field (for example, this.textInput).
return (
@ -163,6 +163,7 @@ render() {
/>
);
}
}
```
Then we can focus it elsewhere in our component when needed:

Loading…
Cancel
Save