child 组件在 parent 中响应更新状态
React update state in parent from child components
我有 child 组件从 parent 接收道具,但是在 child 中的事件(单击按钮)我想再次 setState
新道具。因此 parent 将列表中的所有项目传递给 children。在 child 属性中,一个按钮删除列表中的一个项目。但是如何更新状态,以便列表项也从视图中删除。这是我的代码:
const Parent = React.createClass({
getInitialState:function(){
return {
items: []
};
},
componentWillMount:function(){
axios.get('/comments')
.then(function(response) {
this.setState({
items: response.data
})
}.bind(this));
},
render() {
return (
<div>
<Child1 data={this.state.items}/>
</div>
);
}
});
export default Parent;
export default function Child1(props){
return(
<div>
{ props.data.map((comment,id) =>(
<p key={id}>
{comment.name}<Delete data={comment.id}/>
</p>
)
)}
</div>
)
}
class Delete extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
Purchase.Action(this.props.data,'remove');
axios.post('/comments', {
item: this.props.data
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}
render() {
return <Button onClick={this.handleClick}>Delete</Button>;
}
}
module.exports = Delete;
所以虽然在服务器端删除了评论,但我想通过更新状态从组件视图中删除评论。
如果你想从组件中删除评论,你必须更新你的 Parent state
。
为此,您可以在 Parent
组件中创建一个新方法 delete(id)
,您可以在其中从状态中删除已删除的项目。
const Parent = React.createClass({
getInitialState:function(){
return {
items: []
};
},
componentWillMount:function(){
this.setState({
items: [
{id: 1,name: "Name 1"},
{id: 2,name: "Name 2"},
{id: 3,name: "Name 3"}
]
})
},
delete(id){
// axios.post(...)
let items = this.state.items.filter(item => item.id !== id);
this.setState({items});
},
render() {
return (
<div>
<Child1
data={this.state.items}
handleClick={this.delete} // Pass the method here
/>
</div>
);
}
});
function Child1(props){
return(
<div>
{ props.data.map((comment,id) =>(
<p key={id}>
{comment.name}
<Delete
data={comment.id}
handleClick={() => props.handleClick(comment.id)} // Pass the id here
/>
</p>
)
)}
</div>
)
}
class Delete extends React.Component {
constructor(props) {
super(props);
}
render() {
return <button onClick={this.props.handleClick}>Delete</button>;
}
}
我有 child 组件从 parent 接收道具,但是在 child 中的事件(单击按钮)我想再次 setState
新道具。因此 parent 将列表中的所有项目传递给 children。在 child 属性中,一个按钮删除列表中的一个项目。但是如何更新状态,以便列表项也从视图中删除。这是我的代码:
const Parent = React.createClass({
getInitialState:function(){
return {
items: []
};
},
componentWillMount:function(){
axios.get('/comments')
.then(function(response) {
this.setState({
items: response.data
})
}.bind(this));
},
render() {
return (
<div>
<Child1 data={this.state.items}/>
</div>
);
}
});
export default Parent;
export default function Child1(props){
return(
<div>
{ props.data.map((comment,id) =>(
<p key={id}>
{comment.name}<Delete data={comment.id}/>
</p>
)
)}
</div>
)
}
class Delete extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
Purchase.Action(this.props.data,'remove');
axios.post('/comments', {
item: this.props.data
})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}
render() {
return <Button onClick={this.handleClick}>Delete</Button>;
}
}
module.exports = Delete;
所以虽然在服务器端删除了评论,但我想通过更新状态从组件视图中删除评论。
如果你想从组件中删除评论,你必须更新你的 Parent state
。
为此,您可以在 Parent
组件中创建一个新方法 delete(id)
,您可以在其中从状态中删除已删除的项目。
const Parent = React.createClass({
getInitialState:function(){
return {
items: []
};
},
componentWillMount:function(){
this.setState({
items: [
{id: 1,name: "Name 1"},
{id: 2,name: "Name 2"},
{id: 3,name: "Name 3"}
]
})
},
delete(id){
// axios.post(...)
let items = this.state.items.filter(item => item.id !== id);
this.setState({items});
},
render() {
return (
<div>
<Child1
data={this.state.items}
handleClick={this.delete} // Pass the method here
/>
</div>
);
}
});
function Child1(props){
return(
<div>
{ props.data.map((comment,id) =>(
<p key={id}>
{comment.name}
<Delete
data={comment.id}
handleClick={() => props.handleClick(comment.id)} // Pass the id here
/>
</p>
)
)}
</div>
)
}
class Delete extends React.Component {
constructor(props) {
super(props);
}
render() {
return <button onClick={this.props.handleClick}>Delete</button>;
}
}