[Related topic recommendations:react interview questions(2020)]
Theme: React
Difficulty: ⭐
Virtual DOM (VDOM)is an in-memory representation of the real DOM. The representation of the UI is kept in memory and synchronized with the actual DOM. This is a step that occurs between the rendering function being called and the element being displayed on the screen. The entire process is calledreconciliation.
Theme: React
Difficulty: ⭐⭐
and lifecycle hooks.
and renders it to the page, it is a stateless component and is a functional component. It is also called a dumb component or a display component.
Theme: React
Difficulty: ⭐⭐
Refs
Provides a way to access the DOM created in therender
method Methods of nodes or React elements. In a typical data flow,props
is the only way for parent and child components to interact. If you want to modify a child component, you need to re-render it with a newpros
. There are exceptions to everything. In some cases, we need to force modification of children outside of the typical data flow. In this case,Refs
can be used.
We can add aref
attribute to the component to use. The value of this attribute is a callback function that receives the underlying DOM element or the mounted instance of the component as its first parameter.
class UnControlledForm extends Component { handleSubmit = () => { console.log("Input Value: ", this.input.value) } render () { return () } }
Please note that theinput
element has aref
attribute whose value is a function. This function receives the actual DOM element of the input and then places it on the instance so that it can be accessed inside thehandleSubmit
function.
It is often misunderstood thatrefs
can only be used within class components, butrefs
can also be used with function components by leveraging closures in JS.
function CustomForm ({handleSubmit}) { let inputElement return () }
Topic: React
Difficulty: ⭐⭐
In order to solve cross-browser issues Compatibility issue,SyntheticEvent
instance will be passed to your event handler function,SyntheticEvent
is React's cross-browser browser native event wrapper, it also has browser native events The same interface, includingstopPropagation()
andpreventDefault()
.
What’s interesting is that React doesn’t actually attach events to the child nodes themselves. React uses a single event listener to listen to all events at the top level. This is good for performance and means React doesn't need to track event listeners when updating the DOM.
Theme: React
Difficulty: ⭐⭐
props
andstate
are ordinary JS objects. Although they both contain information that affects rendered output, their functionality in terms of components is different. That is,
state
is the component’s own data management, control of its own state, and is variable;props
is the data passed in from the outside. Parameters, immutable;state
are called stateless components, and those withstate
are called stateful components;props more
, use lessstate
, that is, write more stateless components.Theme: React
Difficulty: ⭐⭐
Refs are created usingReact .createRef()
Created and attached to the React element via theref
attribute. When constructing a component, you typically assignRefs
to instance properties so that they can be referenced throughout the component.
class MyComponent extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } render() { return; } }
Or use it like this:
class UserForm extends Component { handleSubmit = () => { console.log("Input Value is: ", this.input.value) } render () { return () } }
Theme: React
Difficulty: ⭐⭐
Higher Order Component (HOC)is a function that accepts a component and returns a new component. Basically, this is a pattern derived from the composition feature of React, calledPure Componentsbecause they can accept any dynamically provided subcomponent, but do not modify or copy the input component any behavior in.
const EnhancedComponent = higherOrderComponent(WrappedComponent);
HOC can be used for many of the following use cases
super
in the constructor and passingprops
as a parameter?Topic: React
Difficulty: ⭐⭐
The subclass constructor cannot be used until thesuper()
method is calledthis
Quotes, the same goes for ES6 subclasses. The main reason for passingprops
parameters to thesuper()
call is to be able to obtain the incomingprops through
this.propsin the child constructor
.
Passing props
class MyComponent extends React.Component { constructor(props) { super(props); console.log(this.props); // { name: 'sudheer',age: 30 } } }
Not passing props
class MyComponent extends React.Component { constructor(props) { super(); console.log(this.props); // undefined // 但是 Props 参数仍然可用 console.log(props); // Prints { name: 'sudheer',age: 30 } } render() { // 构造函数外部不受影响 console.log(this.props) // { name: 'sudheer',age: 30 } } }
The above example reveals something. The behavior ofprops
is different only inside the constructor, it is the same outside the constructor.
Theme: React
Difficulty: ⭐⭐⭐
In HTML, form elements such as,
Copy after login
渲染结果:
Copy after login
另一种可能的方法是:
var condition = true; var component = ( );
Copy after login
问题 31:Hooks会取代render props
和高阶组件吗?
主题: React
难度: ⭐⭐⭐⭐
通常,render props
和高阶组件仅渲染一个子组件。React团队认为,Hooks 是服务此用例的更简单方法。
这两种模式仍然有一席之地(例如,一个虚拟的scroller
组件可能有一个renderItem prop
,或者一个可视化的容器组件可能有它自己的 DOM 结构)。但在大多数情况下,Hooks 就足够了,可以帮助减少树中的嵌套。
问题 32:如何避免组件的重新渲染?
主题: React
难度: ⭐⭐⭐⭐
React 中最常见的问题之一是组件不必要地重新渲染。React 提供了两个方法,在这些情况下非常有用:
React.memo()
:这可以防止不必要地重新渲染函数组件
PureComponent
:这可以防止不必要地重新渲染类组件
这两种方法都依赖于对传递给组件的props
的浅比较,如果props
没有改变,那么组件将不会重新渲染。虽然这两种工具都非常有用,但是浅比较会带来额外的性能损失,因此如果使用不当,这两种方法都会对性能产生负面影响。
通过使用React Profiler,可以在使用这些方法前后对性能进行测量,从而确保通过进行给定的更改来实际改进性能。
问题 33:什么是纯函数?
主题: React
难度: ⭐⭐⭐⭐⭐
纯函数是不依赖并且不会在其作用域之外修改变量状态的函数。本质上,纯函数始终在给定相同参数的情况下返回相同结果。
问题 34:当调用setState
时,Reactrender
是如何工作的?
主题: React
难度: ⭐⭐⭐⭐⭐
咱们可以将"render
"分为两个步骤:
- 虚拟 DOM 渲染:当
render
方法被调用时,它返回一个新的组件的虚拟 DOM 结构。当调用setState()
时,render
会被再次调用,因为默认情况下shouldComponentUpdate
总是返回true
,所以默认情况下 React 是没有优化的。
- 原生 DOM 渲染:React 只会在虚拟DOM中修改真实DOM节点,而且修改的次数非常少——这是很棒的React特性,它优化了真实DOM的变化,使React变得更快。
问题 35:如何避免在React重新绑定实例?
主题: React
难度: ⭐⭐⭐⭐⭐
有几种常用方法可以避免在 React 中绑定方法:
1.将事件处理程序定义为内联箭头函数
class SubmitButton extends React.Component { constructor(props) { super(props); this.state = { isFormSubmitted: false }; } render() { return ( ) } }
Copy after login
2.使用箭头函数来定义方法:
class SubmitButton extends React.Component { state = { isFormSubmitted: false } handleSubmit = () => { this.setState({ isFormSubmitted: true }); } render() { return ( ) } }
Copy after login
3.使用带有 Hooks 的函数组件
const SubmitButton = () => { const [isFormSubmitted, setIsFormSubmitted] = useState(false); return ( ) };
Copy after login
本文转载自:https://segmentfault.com/a/1190000020912300
相关教程推荐:React视频教程
Difference
Function component
Class component
Is there
No
Yes
Is there a life cycle
No
Yes
##Is there a state
state
this
No
Yes
The above is the detailed content of 35 interview questions you must know and master React. For more information, please follow other related articles on the PHP Chinese website!
Related labels:
source:segmentfault.com
Previous article:Learn 5 ways to interact with JavaScript and CSS
Next article:How does Vue-Router implement middleware pipeline?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2023-04-26 17:59:18
-
2023-04-26 17:47:48
-
2023-04-26 17:41:42
-
2023-04-26 17:37:05
-
2023-04-26 17:31:25
-
2023-04-26 17:27:32
-
2023-04-25 19:57:58
-
2023-04-25 19:53:11
-
2023-04-25 19:49:11
-
2023-04-25 19:41:54
Latest Issues
Related Topics
More>
-
About us
Disclaimer
Sitemap
-
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!