• +91 9723535972
  • info@interviewmaterial.com

ReactJS Interview Questions and Answers

Related Subjects

Question - How to create refs?

Answer - There are two approaches i. This is a recently added approach. Refs are created using React.createRef() method and attached to React elements via the ref attribute. In order to use refs throughout the component, just assign the ref to the instance property within constructor. ii. class MyComponent extends React.Component { iii. constructor(props) { iv. super(props) v. this.myRef = React.createRef() vi. } vii. render() { viii. return

ix. } } x. You can also use ref callbacks approach regardless of React version. For example, the search bar component's input element accessed as follows, xi. class SearchBar extends Component { xii. constructor(props) { xiii. super(props); xiv. this.txtSearch = null; xv. this.state = { term: '' }; xvi. this.setInputSearchRef = e => { xvii. this.txtSearch = e; xviii. } xix. } xx. onInputChange(event) { xxi. this.setState({ term: this.txtSearch.value }); xxii. } xxiii. render() { xxiv. return ( xxv. xxix. ); xxx. } } You can also use refs in function components using closures. Note: You can also use inline ref callbacks even though it is not a recommended approach

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners