19 Jun Top 25 ReactJS Interview Questions and Answers
The top 25 ReactJS Interview Questions and Answers are given here. The level of questions is suitable for beginners as well advanced React developers.
Let’s begin!
Ques 1. When do we use data-prefix in React?
- To pass date from one component to another
- To add custom attributes
- To add JavaScript expression
- To initiate the component’s properties
Ques 2. How props are sent into the component in React?
- Props are sent into the component as attributes
- Props are sent into the component as objects
- Send using super()
- Send props as a JavaScript Expression
Ques 3. The componentās name in React always begins with:
- Lowercase letter
- Uppercase letter
Ques 4. How to specify string literals as attributes in React?
- const element = <div tabIndex=ā20ā></div>;
- const element = <div tabIndex=”20″></div>;
- setState({tabIndex: “20”})
- setState({tabIndex: ā20ā})
Ques 5. Ā Is it a good approach to insert some of the components in separate files?
- TRUE: It helps in code re-usability.
- FALSE: It wonāt be of much use later on.
Ques 6. Will this re-render the component in React?
1 2 3 |
this.state.sports = 'Cricket'; |
- TRUE
- False
Explanation:
To re-render a component in React, use setState():
1 2 3 |
this.setState({sports: 'Cricket'}); |
Ques 7. Ā Which method is used to update the state of the component?
- render()
- componentDidUpdate()
- setState() method
- componentWillUpdate()
Ques 8. What are the phases in the React Lifecycle of Components?
- Phase I: Mount, Phase2: Receive Props, Phase 3: Unmount
- Phase I: Mount, Phase2: Render, Phase3: Update, Phase: Unmount
- Phase I: Mount, Phase2: Update, Phase 3: Unmount
- Phase I: Mount, Phase2: Update, Phase 3: Unmount
Ques 9. JSX converts
- HTML tags into react elements
- JavaScript into HTML
- XML into react elements
- JavaScript into react elements
Ques 10. With JSX, expressions can be written inside __ ?
- angle brackets <>
- double quotes
- single quotes
- curly braces { }
Ques 11. Consider the following program and predict the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import React from 'react'; import ReactDOM from 'react-dom'; class Demo extends React.Component { constructor(props) { super(props); this.state = {topplayer: "david"}; } static getDerivedStateFromProps(props, state) { return {topplayer: props.firstrank }; } render() { return ( <h1>Rank 1 of cricketer {this.state.topplayer}</h1> ); } } ReactDOM.render(<Demo firstrank="amit"/>, document.getElementById('root')); |
- ) Rank 1 of cricketer amit
- ) Rank 1 of cricketer david
The answer is (1) since the getDerivedStateFromProps method gets called right before the render method and display amit.
Ques 12. Ā For integration with other JavaScript frameworks, what is used in React?
- componentDidMount
- componentWillMount
- componentWillUpdate
- componentWillReceiveProps
Ques 13. If JSX is not necessary to use, then why it is still considered in React? Select any of the options supporting the statement:
- Useful since it converts HTML tags into react elements
- Performs optimization and is also type safe
- We can use custom attributes
- Since JavaScript expressions can be used inside of JSX
Ques 14. Are React Props read-only?
- TRUE
- FALSE
Ques 15. While sending a variable to a component in React, where it should be placed? Select the immediate answer.
- Inside angle brackets
- Wrap it in HTML
- Pass it through document.getElementById()
- Inside curly brackets
Ques 16. The _______ method is called when a component gets updated
- shouldComponentUpdate()
- render() method
- getDerivedStateFromProps()
- constructor()
Ques 17. A component doesnāt get updated when there is a change in the component’s state or props?
- TRUE
- FALSE
Explanation:
Under React Lifecycle, the 1st phase is Mounting and the 2nd is Updating. A component gets updated whenever there is a change in the component’s state or props. Following are the methods that is called when a component is updated: getDerivedStateFromProps(), shouldComponentUpdate(), render(), getSnapshotBeforeUpdate(), componentDidUpdate(), etc.
Ques 18. Consider the following program?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Team extends React.Component { render() { return <p>WorldCup winners {this.props.teamname}!</p>; } } class Result extends React.Component { render() { const winningTeam = "India"; return ( <div> <h2>The winning team?</h2> <Team teamname={winningTeam} /> </div> ) } } ReactDOM.render(<Result />, document.getElementById('root')); |
The output is as follows:
Is the above program sending a variable to a component correctly?
- TRUE
- FALSE
Ques 19. Predict the output of the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import React from 'react'; class App extends React.Component { render() { return ( <div> <h2>{25+25}</h2> </div> ); } } export default App; |
- ) 2525
- ) 50
The answer is (2) since expressions are written inside curly braces { } with JSX.
Ques 20. Is the below code using JSX in React?
1 2 3 4 5 6 7 8 |
import React from 'react'; import ReactDOM from 'react-dom'; const demo = React.createElement('h2', {}, 'This is demo heading2'); ReactDOM.render(demo, document.getElementById('root')); |
- TRUE
- FALSE
Explanation:
No, the above code isnāt using JSX. Following is the above code with JSX:
1 2 3 4 5 6 7 8 |
import React from 'react'; import ReactDOM from 'react-dom'; const demo = <h2>This is demo heading2</h2>; ReactDOM.render(demo, document.getElementById('root')); |
Ques 21. Consider the below code snippet. Is this the correct way to display a component in the ārootā element?
1 2 3 |
ReactDOM.render(<Team />, document.getElementById('root')); |
- TRUE
- FALSE
Ques 22. A constructor() function, if present in the component, gets called when the component gets ________?
- Updated
- Initiated
- Unmounted
- All of the above
Ques 23. Why does a component require a render() method?
- To return HTML
- To set state
- To call another component
- All of the above
Ques 24. Why we cannot define setState() inside render() function?
- The changes wonāt get added to the original state
- Since it updates the state manually.
- setState() replaces the state
- It will lead to an infinite loop since defining it will call the render again.
Ques 25. React batch multiple _____ calls into a single update for performance
- bind
- render()
- setState()
- forceUpdate()
If you find the ReactJS Interview Questions & Answers, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel:Ā Join Now
Read More:
- 100 Android Interview Questions and Answers
- Java Interview Questions and Answers
- Java Basics Interview Questions
- Java Quiz
- Android Quiz
- jQuery Quiz
- HTML5 Quiz
- CSS3 Quiz
No Comments