React js interview questions

React is the most popular frontend library of framework right now and if it keeps evolving often.

It has some many tricks under the hood that even experienced professionals fails at interviews.

In this set we have curated list of react js interview questions and answers which are differentiated topic wise. Each question will have its topic’s tag and difficulty label.

1. Which among the following is the correct way of rendering HTML in react?

//1
render() {
  return false
}

//2
render() {
  return null
}

//3
render() {
  return []
}

//4
render() {
  return <React.Fragment></React.Fragment>
}

//5
render() {
  return <></>
}
ReactRenderingComponenteasy

Options

  • First
  • Second
  • Third and Fourth
  • All of them
  • None of them

Answer

1, 2, 3, 4


In react we can return falsy values to render nothing


Is this a correct way to print JSON as it is in react?

import React, {Component} from 'react';

const data = { name: 'Prashant', age: 24 }

class App extends Component {
  render() {
    return (
      <pre>
        {JSON.stringify(data, null, 2)}
      </pre>
    )
  }
}

export default App;
ReactRenderingeasy

Options

  • NO. JSON cannot be printed in react.
  • NO. JSON can be printed without PRE tag.
  • YES. JSON can be printed with PRE tag like this.
  • NO. JSON is not allowed in react.
  • YES. JSON.stringify prints the JSON data as it is.

Answer

1, 3


JSON.stringify() converts a json to a string which can then be shown in pre tag.