Member-only story
Mastering the ReactJS Interview: Top 12 Questions
1. Explain React Router and its use in a single page application (SPA)
4 min readJun 21, 2024
Answer: React Router is a standard library for navigation in React applications. It enables the development of SPAs by allowing the creation of routes that map to different components.
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
const App = () => (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
</nav>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
</div>
</Router>
);
2. What is Redux and how does it work with React?
Answer: Redux is a state management library that helps manage the state of a React Application. It mantains a single source of truth (the store) and uses actions and reducers to update the state.
// Action
const increment = () => ({
type: 'INCREMENT',
});
// Reducer
const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
default:
return state;
}
};
// Store
const store = createStore(counterReducer);
// React Component
const CounterComponent = () => {
const count = useSelector((state) => state);
const dispatch = useDispatch();
return (…