React Interview
React Interview
Basic concepts
Component vs Element
● Component: used to build your block of UI.
○ Class component
○ Functional Component
The “return” of render() in class component or in functional component is
telling what the DOM should look like when this component is rendered in browser
● Element: an object that is returned from component, this is what your see as
UI in browser -> represent the DOM node that the component represents
React.createElement vs JSX
● React.createElement tells React what the DOM should look like when a
component is rendered
● JSX: allow to write the returned DOM of a component as HTML-like code in
JS, instead of using React.createElement
Composing Components
● A way to write a reusable component
○ props.children
○ pass a function that return components as props
Advanced concepts
Higher-Order Component
● Higher-order function: a function that takes another function as a parameter or
return another function
● Higher-Order Component: is a function that takes a component and return another
component
Example: A Higher Order Component withInput() which can handle all the input type
React Router
Router
Hooks
Hooks is a new addition in React 16.8. They allow to use state, lifecycle methods and other
React features for functional component without writing a class component
Basic hooks:
● useState(initialState) : take an argument and return a current state and a method to
modify state
● useEffect() : purposes like componentDidMount, componentDidUpdate and
componentWillUnmount lifecycle methods
○ useEffect(() => {...}) like componentDidMount and componentDidUpdate
○ useEffect(() => {...}, []) like componentDidMount
○ useEffect(() => {...}, [props1/state1, props2/state2,...]) like
componentDidMount, shouldComponentUpdate
Clean up function in useEffect is used to remove some codes from the precedent
render of useEffect, for example to remove an event listener from the last run of useEffect
Clean up function in useEffect : componentWillUnmount()
Other Hooks
● useReducer(reducer, initialArg, init)
○ reducer : (state, action) => newState
○ return [state, dispatch]
● useContext(): consume data from a Context object that has been created from
‘createContext()’ method -> It is used to share data that can be considered "global"
within a component tree without having to pass props down to every level.
● useCallback(): return a memoized callback function and it is used when you want to
memoize a callback function to prevent unnecessary re-renders
This is useful when you have a function in a parent component that is used as props
in its child component and that function is re-created every time the parent
component re-renders, causing unnecessary re-renders of the child children. It is
usually used with useEffect to avoid the unnecessary re-render
In the example below, ‘handleClick’ function is only re-created if ‘onClick’ function changes
● useRef()
Custom Hooks
Context
Ref
Redux
Basic concepts
Advanced concepts