Home Web Front-end JS Tutorial What are the problems encountered by react? Summary of react problems

What are the problems encountered by react? Summary of react problems

Sep 11, 2018 pm 02:49 PM

This article mainly talks about the application of react, and also summarizes the causes of some problems. Now let us take a look at this article

Problems encountered in the react project:

1. Create a new subcomponent and insert it into the parent component. The result rendered by the subcomponent is an XML tag named after the subcomponent, not the return result of the render method in the subcomponent.

Under normal circumstances, the result rendered by the subcomponent is the return result of the render method in the subcomponent, which is the p tag.

Cause of the problem: The naming rules of react components, the first letter must be capitalized.

React’s JSX syntax uses uppercase and lowercase conventions to distinguish local component classes and HTML tags. But pay attention to two points: In JSX syntax, the class and for attributes of HTML tags and component tags must use className and htmlFor as the corresponding attributes.

2. Application built with react, the values ​​of input element and textarea element How to get it and how to update it to the attribute (value) of the element in real time.

If no processing is done like the HTML page, write data to these two elements, and then pass $(input).val(); or this.refs.input01.value;Two methods to read (get) data, the result is empty.

## Because the naked eye can see input element and textareaelementThe content has been typed, but the element attributes (value) in react are not updated in real time. Special processing is required for the input elements and textarea elements. as follows:

     <input value={this.state.email} onChange={this.emailChange.bind(this)} type="email" maxLength="20"/>
        emailChange(event){                this.setState({email: event.target.value})        }
Copy after login

You need to bind the onChange event on the element, then get the value through the event.target.value method, and then update the component state. When the component state changes, The component will re-render and call the render method. The element will update the value attribute through value={this.state.email} to get the data.

##this.state.email || $(input).val() || this.refs.input.value, there is no difference in getting data, but there is a difference in writing data:

To write invisible data, you can use this .refs.input.value="sometext", or, $(input).val("sometext"), Manipulate elements through the refs attribute, not Call the render method without re-rendering the page;

Writing visible data must be controlled by the state attribute of react:

<input value={this.state.email}/>
    this.setState({email: event.target.value})
Copy after login

3. After writing a page, an empty page is rendered without any results.

Cause of the problem: The coding specification for the return value of the render method in the component. There must be code on the same line of the return keyword, either a left p or a left parenthesis. (Related question: Another question reported an error once. The official website explains that the rerun return value cannot be a variable surrounded by curly braces {}. The encoding specification is that the outermost layer must be an XML tag, and there can only be one. There cannot be sibling XML tags. )

4. Passing parameters from parent to child components

Scenario 1: Parent component A needs to pass parameters to child component B

When the parent component calls sub-component B, add the parameters that need to be passed on the element. At the same time, declare and Initialize the property this.props.isdisplay with the same name Incoming data.

Scenario 2: Parent component A needs to pass parameters to grandchild component C. There is sub-component B in the middle, and B calls component C

A调用C的时候,需要传入属性isdisplay,此时需要在B组件的props属性中声明并初始化isdisplay属性,同时在B组件调用C组件时,在C组件的属性中传入isdisplay属性。

5、写好一个组件页面,浏览器没有渲染出任何结果,显示空白页面。

问题原因:react的render方法语法格式错误

render(){}方法中的return返回内容格式要求,和return同一行必须有内容,并且返回的标签元素最外层只能有一个元素,形如:

return (
<p>...</p>
)
retuern <p>
...
</p>
Copy after login

6、组件的setState方法,传入参数详解,什么时候需要用回调函数?

this.setStete({},()=>{});//组件状态量,回调函数

如下情况,由于setStete方法是异步执行,所以直接在setStete方法后取重置的state状态量,有可能取到的是重置之前的值,因此需要用到回调函数,保证了state状态量重置成功后再取值,肯定取到的是重置后的值。(想看更多就到PHP中文网React参考手册栏目中学习)

this.setState({    pageNum: this.state.pageNum + 1});
console.log(pageNum);//pageNum || pageNum + 1
Copy after login

改造如下:

this.setState({
pageNum: this.state.pageNum + 1
},()=>{
console.log(
pageNum
);
});
Copy after login

总结:回调函数存在的功能,异步方法执行成功后再执行回调函数内容,即“异步方法同步化”。

7、组件状态量的初始化两种方法:

ES6语法中,可以在constructor方法中初始化,如下:

constructor(super){
    props(super);
    this.state({
        name: "nickname",
    });
}
Copy after login

react原生语法如下:

getInitialState() {    return {
        name: "nickname",
    };}
Copy after login

8、react组件的结构与数据和逻辑无关

    选用react框架搭建的web应用,最高效的事情就是,组件可以一次封装,多次调用,也就是说开发的所有组件都是可复用高复用度组件。

    因此,需要理清一个思路,数据与逻辑和组件结构无关,对于具体需求的页面,可以用多个组件拼装而成,每个组件没有具体的数据和逻辑,页面的数据和逻辑在所有组件布局成功之后再添加。

9、对于list页面的路由配置

    场景:比如员工信息列表,每一项对应一个人的详情页面,详情页面的布局完全相同

    这种情况下需要用到react-router的路由参数功能,在路由配置的时候,增加一条参数配置

path: 'employee'//列表页路由配置
path: 'detail/:employeeId'//详情页路由配置,path 属性中的 :employeeId就是该路由的参数params

employeeId}>//列表每项跳转路径
this.props.params.employeeId;//详情页路由具体实现,React Router 将路由的数据都通过 props传递给了页面组件,从而可以访问路由相关数据了。

10、遇到Maximum call stack size exceeded栈溢出问题总结

    案例一:语法逻辑错误

11. Control of the number of component state variables: The value obtained by the operation of the defined state cannot be defined as a state variable again

This article ends here (if you want to see more, go to the PHP Chinese website React User Manual column to learn). If you have any questions, you can leave them below Leave a message with a question.

The above is the detailed content of What are the problems encountered by react? Summary of react problems. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

See all articles