无法更新子组件中的道具
Unable to update props in child component
这是我的具有状态(值和项目)的父组件。我正在尝试将值状态作为道具传递给子组件。当我单击按钮时,渲染方法中执行的代码是执行切换。但是当我在 componentDidMount 中调用 list 函数时,Toggle 不起作用但执行了点击事件。
import React, { Component } from 'react'
import Card from './Components/Card/Card'
export class App extends Component {
state = {
values : new Array(4).fill(false),
item : [],
}
toggleHandler = (index) => {
console.log("CLICKED");
let stateObject = this.state.values;
stateObject.splice(index,1,!this.state.values[index]);
this.setState({ values: stateObject });
}
list = () => {
const listItem = this.state.values.map((data, index) => {
return <Card key = {index}
show = {this.state.values[index]}
toggleHandler = {() => this.toggleHandler(index)} />
})
this.setState({ item : listItem });
}
componentDidMount(){
// if this is not executed as the JSX is render method is executed everything is working fine. as props are getting update in child component.
this.list();
}
render() {
return (
<div>
{/* {this.state.values.map((data, index) => {
return <Card key = {index}
show = {this.state.values[index]}
toggleHandler = {() => this.toggleHandler(index)} />
})
} */}
{this.state.item}
</div>
)
}
}
export default App
这是我的子组件,状态作为道具传递
import React from 'react'
const Card = (props) => {
return (
<div>
<section>
<h1>Name : John Doe</h1>
<h3>Age : 20 </h3>
</section>
{props.show ?
<section>Skills : good at nothing</section> : null
}
<button onClick={props.toggleHandler} >Toggle</button>
</div>
)
}
export default Card
我知道 componentDidMount 只执行一次。但是除了直接在 render 方法中编写 JSX 之外如何让它工作
复制状态而不是直接改变它。通过使用 [...this.state.values] 或 this.state.values.slice()
toggleHandler = (index) => {
console.log("CLICKED");
let stateObject = [...this.state.values]
stateObject = stateObject.filter((_, i) => i !== index);
this.setState({ values: stateObject });
}
同样在你的渲染方法中,this.state.item是一个数组所以你需要循环它
{this.state.item.map(Element => <Element />}
也可以直接在你的渲染方法中做
{this.state.values.map((data, index) => {
return <Card key = {index}
show = {this.state.values[index]}
toggleHandler = {() => this.toggleHandler(index)} />
})}
在您的卡片组件中尝试使用
<button onClick={() => props.toggleHandler()}} >Toggle</button>
值应映射到 class 组件的 render()
内部才能工作
像这样:
render() {
const { values } = this.state;
return (
<div>
{values.map((data, index) => {
return (
<Card
key={index}
show={values[index]}
toggleHandler={() => this.toggleHandler(index)}
/>
);
})}
</div>
);
}
检查演示沙箱
https://codesandbox.io/s/stupefied-spence-67p4f?file=/src/App.js
这是我的具有状态(值和项目)的父组件。我正在尝试将值状态作为道具传递给子组件。当我单击按钮时,渲染方法中执行的代码是执行切换。但是当我在 componentDidMount 中调用 list 函数时,Toggle 不起作用但执行了点击事件。
import React, { Component } from 'react'
import Card from './Components/Card/Card'
export class App extends Component {
state = {
values : new Array(4).fill(false),
item : [],
}
toggleHandler = (index) => {
console.log("CLICKED");
let stateObject = this.state.values;
stateObject.splice(index,1,!this.state.values[index]);
this.setState({ values: stateObject });
}
list = () => {
const listItem = this.state.values.map((data, index) => {
return <Card key = {index}
show = {this.state.values[index]}
toggleHandler = {() => this.toggleHandler(index)} />
})
this.setState({ item : listItem });
}
componentDidMount(){
// if this is not executed as the JSX is render method is executed everything is working fine. as props are getting update in child component.
this.list();
}
render() {
return (
<div>
{/* {this.state.values.map((data, index) => {
return <Card key = {index}
show = {this.state.values[index]}
toggleHandler = {() => this.toggleHandler(index)} />
})
} */}
{this.state.item}
</div>
)
}
}
export default App
这是我的子组件,状态作为道具传递
import React from 'react'
const Card = (props) => {
return (
<div>
<section>
<h1>Name : John Doe</h1>
<h3>Age : 20 </h3>
</section>
{props.show ?
<section>Skills : good at nothing</section> : null
}
<button onClick={props.toggleHandler} >Toggle</button>
</div>
)
}
export default Card
我知道 componentDidMount 只执行一次。但是除了直接在 render 方法中编写 JSX 之外如何让它工作
复制状态而不是直接改变它。通过使用 [...this.state.values] 或 this.state.values.slice()
toggleHandler = (index) => {
console.log("CLICKED");
let stateObject = [...this.state.values]
stateObject = stateObject.filter((_, i) => i !== index);
this.setState({ values: stateObject });
}
同样在你的渲染方法中,this.state.item是一个数组所以你需要循环它
{this.state.item.map(Element => <Element />}
也可以直接在你的渲染方法中做
{this.state.values.map((data, index) => {
return <Card key = {index}
show = {this.state.values[index]}
toggleHandler = {() => this.toggleHandler(index)} />
})}
在您的卡片组件中尝试使用
<button onClick={() => props.toggleHandler()}} >Toggle</button>
值应映射到 class 组件的 render()
内部才能工作
像这样:
render() {
const { values } = this.state;
return (
<div>
{values.map((data, index) => {
return (
<Card
key={index}
show={values[index]}
toggleHandler={() => this.toggleHandler(index)}
/>
);
})}
</div>
);
}
检查演示沙箱
https://codesandbox.io/s/stupefied-spence-67p4f?file=/src/App.js