[英]Trying to pass state value from child to parent, getting Maximum Update Depth Exceeded error
I have a class based React component that receives an array of objects and then filters that array to return a new array of objects with values that match the value of a name
element.我有一个基于 class 的 React 组件,它接收一个对象数组,然后过滤该数组以返回一个新的对象数组,其值与name
元素的值匹配。
I'm setting the value for the name
element from within a child component and trying to pass that piece of state up to the parent.我正在从子组件中设置name
元素的值,并尝试将 state 传递给父组件。
My issue is that I can pass the state no problem and can console.log()
the changes, but when I call this.setState({ name: value )}
I start getting the Maximum Update Depth Exceeded
error and I'm not where I'm going wrong.我的问题是我可以毫无问题地通过 state 并且可以console.log()
进行更改,但是当我调用this.setState({ name: value )}
时,我开始收到Maximum Update Depth Exceeded
错误而且我不在哪里我错了。
I have a Codesandbox for live example and here are my components:我有一个用于实时示例的Codesandbox ,这是我的组件:
Parent Component
import React from "react";
import "./styles.css";
import MenuButton from "./MenuButton";
const data = [
{
name: "Thing 1",
groupName: "animals"
},
{
name: "Thing 2",
groupName: "animals"
},
{
name: "Thing 3",
groupName: "animals"
},
{
name: "Person 1",
groupName: "people"
},
{
name: "Person 1",
groupName: "people"
},
{
name: "Person 1",
groupName: "people"
}
];
let newData = data.filter((obj) => obj.groupName === name);
console.log("NEW DATA: ", newData);
class App extends React.Component {
constructor(props) {
super(props);
this.handleNameChange = this.handleNameChange.bind(this);
}
state = { name: "" };
handleNameChange = (value) => {
console.log("PARENT VALUE: ", value);
this.setState({ name: value });
};
render() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<MenuButton handleNameChange={this.handleNameChange} />
</div>
);
}
}
export default App;
Child Component
import React, { useState } from "react";
const MenuButton = (props) => {
const [name, setName] = useState("");
props.handleNameChange(name);
return (
<div>
<button
value={"Animals"}
onClick={(e) => setName(e.target.value)}
style={{ marginRight: "25px" }}
>
Animals
</button>
<button value={"Person"} onClick={(e) => setName(e.target.value)}>
Person
</button>
{console.log(name)}
</div>
);
};
export default MenuButton;
That's happening because you are calling props.handleNameChange(name);
发生这种情况是因为您正在调用props.handleNameChange(name);
on every render, so it renders the parent again, that renders the child again (in a loop of infinite renders), just add a useEffect where you call props.handleNameChange(name);
在每次渲染时,它会再次渲染父级,再次渲染子级(在无限渲染循环中),只需在调用props.handleNameChange(name);
like this:像这样:
import React, { useState, useEffect } from "react";
const MenuButton = (props) => {
const [name, setName] = useState("");
useEffect(() => {
props.handleNameChange(name);
}, [name]);
return (
<div>
<button
value={"Animals"}
onClick={(e) => setName(e.target.value)}
style={{ marginRight: "25px" }}
>
Animals
</button>
<button value={"Person"} onClick={(e) => setName(e.target.value)}>
Person
</button>
{console.log(name)}
</div>
);
};
export default MenuButton;
This will only run the handleNameChange
function when the name variable changes, not every time.这只会在名称变量更改时运行handleNameChange
,而不是每次都运行。
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.