3 Major Asked Differences in ReactJS Interview — Interview Questions
Hamza Siddique
MERN Stack Developer @ Desol Int. | AWS | React.js | React Native (Expo) | Next.js | Node.js | Express.js | Nest.js | TypeScript | MERN Stack | Tailwind CSS | GraphQL | Docker (DevOps) | Angular | MEAN Stack
In this story, we will understand those 3 major differences in ReactJS that are being asked in almost every interview of ReactJS from beginner to senior level.
1. Difference between Stateful and Stateless Components
Stateful Components
Example:
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
incrementCount = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.incrementCount}>Increment</button>
</div>
);
}
}
export default Counter;
Stateless Components
Example:
import React, { useState } from 'react';
const MyComponent = () => {
// Using useState hook to declare state variables
const [count, setCount] = useState(0);
const incrementCount = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={incrementCount}>Increment</button>
</div>
);
};
export default MyComponent;
2. Difference between Controlled and Un-Controlled Components
Controlled Components
Example:
import React, { useState } from 'react';
function ControlledComponentExample() {
const [inputValue, setInputValue] = useState('');
const handleChange = (e) => {
setInputValue(e.target.value);
};
return (
<div>
<input
type="text"
value={inputValue}
onChange={handleChange}
/>
<p>Input value: {inputValue}</p>
</div>
);
}
export default ControlledComponentExample;
领英推荐
Un-Controlled Components
Example
import React, { useRef } from 'react';
function UncontrolledComponentExample() {
const inputRef = useRef(null);
const handleSubmit = (e) => {
e.preventDefault();
console.log('Input value:', inputRef.current.value);
};
return (
<div>
<form onSubmit={handleSubmit}>
<input type="text" ref={inputRef} />
<button type="submit">Submit</button>
</form>
</div>
);
}
export default UncontrolledComponentExample;
3. Difference between Redux Toolkit and Context API
Redux Toolkit
Example
// Redux Toolkit setup
import { configureStore, createSlice } from '@reduxjs/toolkit';
// Define a slice
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: state => {
state.value += 1;
},
decrement: state => {
state.value -= 1;
},
},
});
// Configure the Redux store
const store = configureStore({
reducer: {
counter: counterSlice.reducer,
},
});
export const { increment, decrement } = counterSlice.actions;
export default store;
Context API
Example
// Context setup
import React, { createContext, useContext, useReducer } from 'react';
// Create a context
const CounterContext = createContext();
// Create a provider
export const CounterProvider = ({ children }) => {
const [count, dispatch] = useReducer((state, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}, 0);
return (
<CounterContext.Provider value={{ count, dispatch }}>
{children}
</CounterContext.Provider>
);
};
// Custom hook to use the context
export const useCounter = () => useContext(CounterContext);
Conclusion
Mastering the disparities between stateful and stateless components, controlled and uncontrolled components, and Redux Toolkit versus Context API is pivotal for ReactJS developers facing interviews. These distinctions reflect the nuanced approaches to state management, component behavior, and global versus local state handling. By grasping these differences, candidates can demonstrate their comprehension of fundamental ReactJS concepts, showcasing their ability to make informed architectural decisions and select appropriate tools for diverse project requirements.
Happy Coding!