Interview Questions In ReactJs
Q: What is React and why is it used?
A: React is a JavaScript library used for building user interfaces. It was developed by Facebook and is now maintained by a community of developers. React allows developers to create reusable UI components and manage state in a more efficient and scalable way. It is widely used because of its simplicity, flexibility, and performance.
Q: How do you create a React component?
A: A React component can be created by defining a JavaScript class that extends the React.Component class or by using a function that returns a JSX element.
Example class-based component:
javascriptimport React, { Component } from 'react';
class MyComponent extends Component {
render() {
return <div>Hello, World!</div>;
}
}
Example function-based component:
javascriptimport React from 'react';
function MyComponent() {
return <div>Hello, World!</div>;
}
Q: What is JSX in React?
A: JSX is a syntax extension for JavaScript that allows developers to write HTML-like code in their JavaScript files. It allows developers to create React components that look like HTML templates, but are actually JavaScript functions. JSX makes it easier to read and write code and improves the overall developer experience.
Example JSX code:
javascriptimport React from 'react';
function MyComponent() {
return (
<div> <h1>Hello, World!</h1> <p>This is a paragraph.</p> </div>
);
}
Q: How do you handle events in React?
A: Events in React are handled by adding event handlers to JSX elements using the onEventName
attribute. Event handlers are functions that are executed when a specific event occurs, such as a button click or a form submission.
Example event handling code:
javascriptimport React, { useState } from 'react';
function MyComponent() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<div>
<p>You clicked the button {count} times.</p>
<button onClick={handleClick}>Click me!</button>
</div>
);
}
Q: What is the difference between state and props in React?
A: Props and state are both used to pass data to React components, but they are used in different ways. Props are used to pass data from a parent component to a child component, while state is used to manage data within a component itself.
Props are read-only and cannot be changed by the child component. State, on the other hand, can be changed by the component itself using the setState()
method.
Example code:
javascriptimport React from 'react';
function ParentComponent() {
const message = 'Hello, World!';
return (
<ChildComponent message={message} />
);
}
function ChildComponent(props) {
return (
<div>{props.message}</div>
);
}
In this example, the ParentComponent
passes the message
prop to the ChildComponent
, which displays it in a div
element. The ChildComponent
cannot change the value of the message
prop.
Example code using state:
Q: What are Synthetic events in React?javascriptimport React, { useState } from 'react';
function MyComponent() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return ( <div> <p>You clicked the button {count} times.</p> <button onClick={handleClick}>Click me!</button> </div> );
}
Synthetic events in ReactJS are a cross-browser wrapper around the native browser events. They are implemented by React to make event handling consistent and compatible across different browsers.
One of the most important features of SyntheticEvents is that they are pooled. This means that after the event handler function is called, the SyntheticEvent object is reused and its properties are reset to their initial values. This helps to reduce memory usage and improve performance.
javascriptimport React from 'react';
function MyComponent() {
function handleClick(event) {
event.preventDefault();
console.log('Button clicked!');
}
return (
<button onClick={handleClick}>Click me!</button>
);
}