Introduction
Are you preparing for a React.js interview? You're in the right place! This comprehensive guide covers 15 crucial React concepts you need to know, complete with code examples and practical explanations.
Table of Contents
React.js Fundamentals
Q1: What is React.js and what are its key features? π€
React.js is an open-source JavaScript library developed by Facebook for building user interfaces. Here's what makes it special:
Key Features:
- Virtual DOM π
// React handles DOM updates efficiently
const element = <h1>Hello, React!</h1>;
- Component-Based Architecture ποΈ
// Function Component
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
// Class Component
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
State and Props
Q2: What's the difference between state and props? π€
Understanding the distinction is crucial for React development:
Props Example:
// Parent Component
function Parent() {
return <Child name="John" age={25} />;
}
// Child Component
function Child(props) {
return (
<div className="user-card">
<p>Name: {props.name}</p>
<p>Age: {props.age}</p>
</div>
);
}
React Hooks
Q3: What are React Hooks? Explain the most commonly used hooks. π£
Hooks revolutionized how we write React components. Here are the essentials:
// useState Example
function NameInput() {
const [name, setName] = useState('');
return (
<input
value={name}
onChange={(e) => setName(e.target.value)}
className="input-primary"
/>
);
}
// useEffect Example
function UserProfile({ userId }) {
const [user, setUser] = useState(null);
useEffect(() => {
fetchUser(userId).then(data => setUser(data));
}, [userId]);
return user ? <div>{user.name}</div> : <div>Loading...</div>;
}
Component Lifecycle
Q4: Explain the React Component Lifecycle methods. βοΈ
Modern React offers two approaches to lifecycle management:
Class Components:
class LifecycleComponent extends React.Component {
componentDidMount() {
console.log('Component mounted');
}
componentDidUpdate(prevProps) {
if (this.props.id !== prevProps.id) {
console.log('Props changed');
}
}
componentWillUnmount() {
console.log('Component will unmount');
}
}
Context API
Q5: What is the Context API and when should you use it? π
Context provides a way to share values between components without prop drilling:
const ThemeContext = React.createContext('light');
function App() {
const [theme, setTheme] = useState('light');
return (
<ThemeContext.Provider value={theme}>
<ThemedButton />
</ThemeContext.Provider>
);
}
React Router
Q6: What is React Router and how does it work? π£οΈ
import { BrowserRouter, Route, Switch, Link } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
</BrowserRouter>
);
}
Virtual DOM
Q7: How does React's Virtual DOM improve performance? π
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h1>Count: {count}</h1>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}
React Fragments
Q8: What are React Fragments and why do we need them? π§©
// Better way using Fragments
function ListItems() {
return (
<>
<li>Item 1</li>
<li>Item 2</li>
</>
);
}
Higher-Order Components
Q9: What are Higher-Order Components (HOC)? π
function withAuth(WrappedComponent) {
return function WithAuthComponent(props) {
const [isAuthenticated] = useState(true);
if (!isAuthenticated) {
return <Navigate to="/login" />;
}
return <WrappedComponent {...props} />;
};
}
useCallback Hook
Q10: When and why should you use useCallback? π£
function ParentComponent() {
const [count, setCount] = useState(0);
const memoizedCallback = useCallback(() => {
console.log(count);
}, [count]);
return <ChildComponent onClick={memoizedCallback} />;
}
Error Boundaries
Q11: How do Error Boundaries work in React? π‘οΈ
class ErrorBoundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError(error) {
return { hasError: true };
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong</h1>;
}
return this.props.children;
}
}
useReducer Hook
Q12: When would you use useReducer over useState? π
const initialState = { count: 0 };
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
default:
return state;
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<button onClick={() => dispatch({ type: 'increment' })}>
Count: {state.count}
</button>
);
}
React Portals
Q13: What are React Portals and when would you use them? πͺ
function Modal({ children }) {
return ReactDOM.createPortal(
<div className="modal">
{children}
</div>,
document.getElementById('modal-root')
);
}
Strict Mode
Q14: What benefits does React's Strict Mode provide? π
function App() {
return (
<React.StrictMode>
<div>
<Header />
<Main />
<Footer />
</div>
</React.StrictMode>
);
}
Code Splitting
Q15: How does Suspense enable better code splitting? π¦
const LazyComponent = lazy(() => import('./LazyComponent'));
function App() {
return (
<Suspense fallback={<Loading />}>
<LazyComponent />
</Suspense>
);
}
Wrap Up π
Congratulations! You've covered the most important React concepts for your interview. Remember:
Focus on understanding the concepts deeply
Practice implementing these patterns
Think about real-world use cases
Be ready to explain your reasoning
Interview Tips π‘
Start with simple explanations, then dive deeper
Use practical examples from your experience
Discuss trade-offs and best practices
Show enthusiasm for learning and problem-solving
What's Next?
Stay tuned for next Part, where we'll cover:
Performance optimization techniques
Testing strategies
Advanced state management
Server-side rendering
And much more!
Did you find this guide helpful? Follow me for more React tips and tutorials! Don't forget to like and share if you found this useful.
Tags: #React #JavaScript #WebDevelopment #Programming #TechnicalInterview #ReactJS #FrontEnd #Coding #Interview #WebDev
Performance optimization
Testing strategies
State management patterns
And much more!
Connect with me
If you have any questions about my projects or want to discuss potential collaboration opportunities, please don't hesitate to connect with me. You can reach me through the following channels:
Email: bodheeshvc.developer@gmail.com
LinkedIn: linkedin.com/in/bodheeshvc
Youtube: youtube.com/@BodhiTechTalks
Medium: medium.com/@bodheeshvc.developer
I'm always happy to connect with other professionals in the tech industry and discuss ways to work together. Feel free to reach out and let's see how we can help each other grow and succeed!