├── .github
└── FUNDING.yml
├── LICENSE
├── index.js
├── readme.md
└── zh-cn.md
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [Pau1fitz]
4 | patreon: # Replace with a single Patreon username
5 | open_collective: # Replace with a single Open Collective username
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
12 | polar: # Replace with a single Polar username
13 | buy_me_a_coffee: paulfitz
14 | thanks_dev: # Replace with a single thanks.dev username
15 | custom: # ['link1', 'link2']
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Paul Fitzgerald
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react'
2 | import ReactDOM from 'react-dom'
3 |
4 | class Example extends Component {
5 | render (
6 |
7 |
15 |
16 | )
17 |
18 | ReactDOM.render(
19 | ,
20 | document.getElementById('app')
21 | )
22 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # React Interview Questions
2 |
3 | Chinese version [here](https://github.com/Pau1fitz/react-interview/blob/master/zh-cn.md).
4 |
5 | Below is a list of common React interview questions.
6 |
7 | - [How does React work?](#how-does-react-work)
8 | - [What are the advantages of using React?](#what-are-the-advantages-of-using-react)
9 | - [What is the difference between a Presentational component and a Container component?](#what-is-the-difference-between-a-presentational-component-and-a-container-component)
10 | - [What are the differences between a class component and functional component?](#what-are-the-differences-between-a-class-component-and-functional-component)
11 | - [What is the difference between state and props?](#what-is-the-difference-between-state-and-props)
12 | - [What are the different lifecycle methods?](#what-are-the-different-lifecycle-methods)
13 | - [React hooks](#react-hooks)
14 | - [Where in a React component should you make an AJAX request?](#where-in-a-react-component-should-you-make-an-ajax-request)
15 | - [What are controlled components?](#what-are-controlled-components)
16 | - [What are refs used for in React?](#what-are-refs-used-for-in-react)
17 | - [What is a higher order component?](#what-is-a-higher-order-component)
18 | - [What advantages are there in using arrow functions?](#what-advantages-are-there-in-using-arrow-functions)
19 | - [Why is it advised to pass a callback function to setState as opposed to an object?](#why-is-it-advised-to-pass-a-callback-function-to-setState-as-opposed-to-an-object)
20 | - [What is the alternative of binding `this` in the constructor?](#what-is-the-alternative-of-binding-this-in-the-constructor)
21 | - [How would you prevent a component from rendering?](#how-would-you-prevent-a-component-from-rendering)
22 | - [When rendering a list what is a key and what is its purpose?](#when-rendering-a-list-what-is-a-key-and-what-is-its-purpose)
23 | - [What is the purpose of super(props)?](#what-is-the-purpose-of-superprops)
24 | - [What is JSX?](#what-is-jsx)
25 | - [What is equivalent of the following using React.createElement?](#what-is-equivalent-of-the-following-using-React.createElement)
26 | - [What is `Children`?](#what-is-children)
27 | - [What is state in react?](#what-is-state-in-react)
28 | - [Why would you eject from create-react-app?](#why-would-you-eject-from-create-react-app)
29 | - [What is redux?](#what-is-redux)
30 | - [What is a store in redux?](#what-is-a-store-in-redux)
31 | - [What is an action?](#what-is-an-action)
32 | - [What is a reducer?](#what-is-a-reducer)
33 | - [What is redux thunk used for?](#what-is-redux-thunk-used-for)
34 | - [What is a pure function?](#what-is-a-pure-function)
35 | - [What do you like about React?](#what-do-you-like-about-react)
36 | - [What don't you like about React?](#what-dont-you-like-about-react)
37 | - [Example projects](#example-projects)
38 |
39 |
40 | #### How does React work?
41 | React creates a virtual DOM. When state changes in a component it firstly runs a diffing algorithm, which identifies what has changed in the virtual DOM. The second step is reconciliation, where it updates the DOM with the results of diff.
42 |
43 | #### What are the advantages of using React?
44 | - It is easy to know how a component is rendered, you just need to look at the render function.
45 | - JSX makes it easy to read the code of your components. It is also really easy to see the layout, or how components are plugged/combined with each other.
46 | - You can render React on the server-side. This improves SEO and performance.
47 | - You can use React with any framework you wish as it is only a view layer.
48 | - It is easy to test.
49 |
50 | #### What is the difference between a Presentational component and a Container component?
51 | Presentational components are concerned with how things look. They generally receive data and callbacks exclusively via props. These components rarely have their own state, but when they do it generally concerns UI state, as opposed to data state.
52 |
53 | Container components are more concerned with how things work. These components provide the data and behavior to presentational or other container components. They define actions and provide these as callbacks to the presentational components. They are also often stateful as they serve as data sources.
54 |
55 | #### What are the differences between a class component and functional component?
56 | - Class components allows you to use additional features such as local state and lifecycle hooks. Also, to enable your component to have direct access to your store and thus holds state.
57 |
58 | - When your component just receives props and renders them to the page, this is a 'stateless component', for which a pure function can be used. These are also called dumb components or presentational components.
59 |
60 | #### What is the difference between state and props?
61 |
62 | State is a data structure that starts with a default value when a Component mounts. It may be mutated across time, mostly as a result of user events.
63 |
64 | Props (short for properties) are a Component's configuration. They are received from above and immutable as far as the Component receiving them is concerned. A Component cannot change its props, but it is responsible for putting together the props of its child Components. Props do not have to just be data - callback functions may be passed in as props.
65 |
66 | #### What are the different lifecycle methods?
67 | - `componentWillMount` (deprecated) - this is most commonly used for App configuration in your root component.
68 | - `componentDidMount` - here you want to do all the setup you couldn’t do without a DOM, and start getting all the data you need. Also if you want to set up eventListeners etc. this lifecycle hook is a good place to do that.
69 | - `componentWillReceiveProps` (deprecated) - this lifecyclye acts on particular prop changes to trigger state transitions.
70 | - `shouldComponentUpdate` - if you’re worried about wasted renders `shouldComponentUpdate` is a great place to improve performance as it allows you to prevent a rerender if component receives new `prop`. `shouldComponentUpdate` should always return a boolean and based on what this is will determine if the component is rerendered or not.
71 | - `componentWillUpdate` (deprecated) - rarely used. It can be used instead of `componentWillReceiveProps` on a component that also has `shouldComponentUpdate` (but no access to previous props).
72 | - `componentDidUpdate` - also commonly used to update the DOM in response to prop or state changes.
73 | - `componentWillUnmount` - enables you can cancel any outgoing network requests, or remove all event listeners associated with the component.
74 |
75 | #### React Hooks
76 | Hooks let you use more of React’s features without having to use classes.
77 | The first hook that you will most likely encounter is `useState`. `useState` is a Hook that lets you add React state to function components. It returns an array with a getter and a setter. The syntax looks like `const [count, setCount] = React.useState(0);`. The equivalent when using a class component would be.
78 |
79 | ```js
80 | this.state = {
81 | count: 0
82 | };
83 |
84 |
87 | ```
88 | The next hook you will most likely encounter is `useEffect`. The Effect Hook lets you perform side effects in function components. By passing an empty array as the second argument to `useEffect` is equivalent to using `componentDidMount`. If you pass a value to the array it will only call the `useEffect` function when the value in the array updates.
89 |
90 | ```js
91 | useEffect(() => {
92 | // do stuff when the component mounts
93 | }, []);
94 | ```
95 |
96 | #### Where in a React component should you make an AJAX request?
97 | `componentDidMount` is where an AJAX request should be made in a React component. This method will be executed when the component “mounts” (is added to the DOM) for the first time. This method is only executed once during the component’s life. Importantly, you can’t guarantee the AJAX request will have resolved before the component mounts. If it doesn't, that would mean that you’d be trying to setState on an unmounted component, which would not work. Making your AJAX request in `componentDidMount` will guarantee that there is a component to update.
98 |
99 | #### What are controlled components?
100 |
101 | In HTML, form elements such as ``, `