- React and Redux interview questions
- React
- How does React work?
- What are the advantages of using React?
- What is the difference between a Presentational component and a Container component?
- What are the differences between a class component and functional component?
- What is the difference between state and props?
- Name the different lifecycle methods?
- Where in a React component should you make an AJAX request?
- What are controlled components?
- What are refs used for in React?
- How to create refs?
- What are forward refs?
- What is a higher order component?
- What advantages are there in using arrow functions?
- Why is it advised to pass a callback function to setState as opposed to an object?
- What is the alternative of binding this in the constructor?
- How would you prevent a component from rendering?
- When rendering a list what is a key and what is its purpose?
- What is the purpose of super(props)?
- What is JSX?
- What is equivalent of the following using React.createElement?
- What is Children?
- Why would you eject from create-react-app?
- What is the difference between Element and Component?
- How to create components in React?
- What are Pure Components?
- Why should we not update the state directly?
- What is the difference between HTML and React event handling?
- What are synthetic events in React?
- What is inline conditional expressions?
- Redux
- React
The Basic thought of React is UI = func(data)
, which means UIs are simply a projection of data into a different form of data. And in React, the func
is a (reusable) component. So the basic of React is that developer tell React what the UI should look like based on current state (data). In order to do this render process efficiently, React implement virtual DOM. When a state in the component changes, React use a diff function to check compare the
old VDOM tree and current tree to determine which part needs to be change (reconciliation), and then render the DOM based on this 'patch'.
- Testable, easy to do unit test
- Readable, JSX is easy to read and understand the layout
- Can used with pure js and other framework/library
- Reusable component
- SSR
- backup by larege company (dogfooding) and large community
- Presentational component basicly only receive a props and reture a layout (React Element). They are often stateless, only care about the layout
- Container component: They care more on the logic and data caculation and provide data (props) to presentational component. Also, they are often stateful.
- In class component, you can use state and life cycle hooks.
- In functional component, the component only receive a prop and reture a layout
- Commpon:
- plain JS objects
- trigger render update
- deterministic, which means the same props and state combination for the same component generate the same output
- Difference: In a word, if the component need to change the data at some point, the data should be a state of this component; otherwise props
- props are the configuration of components, it's immutable
- state is serializable representation of one point in time—a snapshot. It's mutable, and also it's private to the component
- state increases complexity and reduces predictability
- mounting
constructor()
static getDerivedStateFromProps()
(rare use)render()
componentDidMount()
- updating
static getDerivedStateFromProps()
(rare use)shouldComponentUpdate()
render()
getSnapshotBeforeUpdate()
componentDidUpdate()
(for side effect)
- unmounting
componentWillUnmount()
In componentDidMount()
. For current component API, this is the only place fit for side effect. In old API, there are also many reason don't put AJAX (and any other side effect) in componentWillMount()
, but the main reason seems to because the implementation of asynchronous rendering (see this issue).
An input form element whose value is controlled by React (instead of DOM) in this way is called a “controlled component”. Value kept in state, and onChange
fire a function to setState()
.
Refs provide a way to access DOM nodes or React elements created in the render method. Use cases:
- Managing focus, text selection, or media playback.
- Triggering imperative animations.
- Integrating with third-party DOM libraries.
Avoid use
refs
if possible.
React.createRef()
- ref callback
Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words, “forward” it) to a child.
A higher-order component is a function that takes a component and returns a new component.
this
binding. Arrow funtions will bind this to lexical scope, so when you translate function in props, it will work correctelly.
Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
ES7 property initializers. Or use arrow function as callback (will create new callback each time the component render).
- in
render
, returnnull
- to avoid unnecessary rerender:
shouldComponentUpdate()
orReact.PureComponent
You should give unque key attribute to each component rendered from list. It help React to decide which element should be changed during reconcilation.
- In ES6, subclass has to call super() if they have constructor
- If you want to access 'props' by
this.props
, you need to send arguments in it
It's a JavaScript extension. It's describe what the layout should looks like in a markup language format. It will be transformed to a normal JS object (React Element).
Q:
const element = (
<h1 className="greeting">
Hello, world!
</h1>
);
A:
React.createElement('h1', {className: 'greeting'}, 'Hello, world!'); // React.createElement(type, [props], [children])
The content between the opening and ending tags of JSX.
To configure Babel and webpack.
- An element is a plain object describing a component instance or DOM node and its desired properties.
- So, component is the function (or class)
- (See this blog)
- functional component
React.Component
React.PureComponent
, it's a component handle shouldComponentUpdate()
. (only shallow check)
setState()
will not only change state but also trigger rerender.
- javascript vs html (and this why inline event listener in HTML is not good practice, you should separate HTML and JS file)
- React event is a cross-brower wrapper around the native event
- HTML event name is lowercase vs React camelCase
- HTML can
return false
to prevent vs React must callevent.preventDefault()
SyntheticEvent is a cross-browser wrapper around the browser's native event. It's API is same as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.
condition && <Element />
It's a library to help web application to manage state.
- Single source of truth (one store)
- State is immutable
- Change must made with pure function
Store is the object bring action together. It store the state and subscribers.
Actions are payloads of information that send data from your application to your store.
Reducers specify how the application's state changes in response to actions sent to the store. It's must be a pure function.
Redux Thunk is a commonly used middleware for asynchronous orchestration. A thunk is a function that returns another function that takes parameters dispatch
and getState
.
A function don't make any side effect and don't mutate input arguments.