Clean • Professional
Props are passed like attributes in JSX.
<ChildComponent propName={value} />No, props are read-only inside child components.
defaultProps defines default values for props when the parent doesn’t provide them.
function Welcome({ name }) {
return <h1>Hello, {name}!</h1>;
}
Welcome.defaultProps = { name: 'Guest' };State is internal, mutable data managed within a component, while props are external, immutable data passed from parent to child.
Use a constructor or class field syntax.
class Counter extends React.Component {
state = { count: 0 };
render() {
return <div>{this.state.count}</div>;
}
}setState() updates the component’s state and triggers a re-render.
this.setState({ count: this.state.count + 1 })It tells useEffect which variables to watch for changes. If empty ([]), it runs once after mount.
Use useEffect with fetch or axios.
useEffect(() => {
fetch('<https://api.example.com/data>')
.then(res => res.json())
.then(setData);
}, []);By using controlled components, where form inputs are bound to React state.
Use the onChange event to update state.
<input value={value} onChange={e => setValue(e.target.value)} />A form element whose value is managed by React state.
A form element that maintains its own state, accessed via ref.
Use useRef (functional) or createRef (class) to access DOM elements.
const inputRef = useRef();
<input ref={inputRef} />;inputRef.current.focus();htmlFor is used in JSX because for is a reserved keyword in JavaScript.
Use a single state object and computed property names.
setForm({ ...form, [e.target.name]: e.target.value });e.preventDefault();<button onClick={handleClick}>Click</button>Yes, using arrow functions.
onClick={() => handleClick(id)}A cross-browser wrapper around native DOM events that works consistently in all browsers.
React events are synthetic, normalized, and part of the React event system.
e.stopPropagation();It’s achieved with controlled components where the input reflects state, and state updates from input.
<button disabled={isDisabled}>Submit</button>items.map(item => <li key={item.id}>{item.name}</li>);It helps React efficiently identify which list items changed, added, or removed.
Yes, components can contain other components as children.
It represents content between a component’s opening and closing tags.
function Wrapper({ children }) {
return <div>{children}</div>;
}By lifting shared state up to the parent and passing it down via props.
Passing props through many nested components unnecessarily to reach a deeply nested one.
Functions that take a component and return an enhanced version with added functionality.
A higher-order function that memoizes functional components, preventing re-renders if props don’t change.
Use React.memo() for functional components or PureComponent for class components.
useEffect runs after rendering (asynchronously), while useLayoutEffect runs before the browser paints (synchronously).
It memoizes functions to prevent re-creation on every render.
It memoizes computed values to avoid expensive recalculations unless dependencies change.
A hook that manages complex state logic using a reducer function, similar to Redux.
A wrapper used in development to highlight potential problems and unsafe practices.
Using React Router (react-router-dom) with components like <BrowserRouter>, <Route>, and <Link>.
Use inline styles, CSS classes, CSS modules, or libraries like styled-components.
className={isActive ? "active" : "inactive"}Return a cleanup function.
useEffect(() => {
const timer = setInterval(() => console.log('Tick'), 1000);
return () => clearInterval(timer);
}, []);