我试图在ReactJS中切换组件的状态,但我得到一个错误说明:
超过最大更新深度。当组件在componentWillUpdate或componentDidUpdate中反复调用setState时,就会发生这种情况。React限制了嵌套更新的数量,以防止无限循环。
我在我的代码中没有看到无限循环,有人能帮我吗?
ReactJS组件代码:
import React, { Component } from 'react';
import styled from 'styled-components';
class Item extends React.Component {
constructor(props) {
super(props);
this.toggle= this.toggle.bind(this);
this.state = {
details: false
}
}
toggle(){
const currentState = this.state.details;
this.setState({ details: !currentState });
}
render() {
return (
<tr className="Item">
<td>{this.props.config.server}</td>
<td>{this.props.config.verbose}</td>
<td>{this.props.config.type}</td>
<td className={this.state.details ? "visible" : "hidden"}>PLACEHOLDER MORE INFO</td>
{<td><span onClick={this.toggle()}>Details</span></td>}
</tr>
)}
}
export default Item;
只需删除(),但如果是useEffect的情况,则
const [isInitialRender, setIsInitialRender] = useState(true);
useEffect(() => {
const data = localStorage.getItem("auth");
如果(isInitialRender) {
setIsInitialRender(假);
If(数据){
const parsed = JSON.parse(data);
setAuth({…认证,用户:已解析。用户,令牌:已解析。令牌});
}
}
}, [auth, isInitialRender]);
isInitialRender true和false将避免你陷入循环
很多好答案,但都遗漏了一些例子,考虑到react/ react native中的钩子
正如上面的回答中所写的,回调不应该在子组件内部被“调用”,而只能被引用。
这意味着什么?
让我们考虑一个父组件,它有2个改变rgb颜色的子组件:
import React, { useState } from "react"
import {View, Text, StyleSheet } from "react-native"
import ColorCounter from "../components/ColorCounter"
const SquareScreen = () =>{
const [red, setRed] = useState(0)
const [blue, setBlue] = useState(0)
const [green, setGreen] = useState(0)
return (
<View>
<ColorCounter
onIncrease={() => setRed(red + 15)}
onDecrease={() => setRed(red - 15)}
color="Red"
/>
<ColorCounter
onIncrease={() => setBlue(blue + 15)}
onDecrease={() => setBlue(blue - 15)}
color="Blue"
/>
<ColorCounter
onIncrease={() => setGreen(green + 15)}
onDecrease={() => setGreen(green - 15)}
color="Green"
/>
<View
style={{
height:150,
width:150,
backgroundColor:`rgb(${red},${blue},${green})`
}}
/>
</View>
)
}
const styles = StyleSheet.create({})
export default SquareScreen
这是子按钮组件:
import React, { useState } from "react"
import {View, Text, StyleSheet, Button } from "react-native"
const ColorCounter = ({color, onIncrease, onDecrease}) =>{
return (
<View>
<Text>{color}</Text>
<Button onPress={onIncrease} title={`Increase ${color}`} /> --> here if you use onPress={onIncrease()} this would cause a call of setColor(either setRed,SetBlue or setGreen) that call again onIncrease and so on in a loop)
<Button onPress={onDecrease} title={`Decrease ${color}`} />
</View>
)
}
export default ColorCounter
1.如果我们想在调用中传递参数,那么我们需要像下面这样调用方法
因为我们使用的是箭头函数,所以不需要在构造函数中绑定方法。
onClick={() => this.save(id)}
当我们像这样在构造函数中绑定方法时
this.save= this.save.bind(this);
然后,我们需要调用该方法而不传递任何参数,如下所示
onClick={this.save}
我们尝试在调用函数时传递参数
如下所示,然后误差就像最大深度超出。
onClick={this.save(id)}