我有2个组成部分。显示和DisplayList。组件协同工作以显示本地存储中的值。一切正常。但是当我激活handleDelete方法时。值将从本地存储中删除,但react不会重新呈现列表。
摘要:我想在激活方法handleDelete()后做出反应以重新渲染displayValues
Display.JSX
import {DisplayList} from './DisplayList';
class Display extends Component {
constructor(props){
let data = JSON.parse(localStorage.getItem('data'));
super(props)
this.state = {
data: data,
}
// Methods
this.displayValues = this.displayValues.bind(this);
}
displayValues(){
return this.state.data.map((data1, index) =>
<DisplayList
key = {index}
email = {data1.email}
password = {data1.password}
/>
)
}
render() {
return (
<ul className="list-group">
{this.displayValues()}
</ul>
)
}
}
DisplayList.JSX
import {Button} from 'react-bootstrap';
export class DisplayList extends Component {
constructor(props){
super(props)
// Methods
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(){
const data = JSON.parse(localStorage.getItem('data'));
for (let index = 0; index < data.length; index++) {
if(this.props.email === data[index].email &&
this.props.password === data[index].password){
data.splice(data[index], 1);
}
}
localStorage.setItem('data', JSON.stringify(data));
}
render() {
return (
<div className = "mt-4">
<li className="list-group-item text-justify">
Email: {this.props.email}
<br />
Password: {this.props.password}
<br />
<Button variant = "info mr-4 mt-1">Edit</Button>
<Button onClick = {this.handleDelete} variant = "danger mt-1">Delete</Button>
</li>
</div>
)
}
}
答案 0 :(得分:1)
实现此目标的一种可能方法是将回调函数作为prop
从Display.JSX
发送到DisplayList.JSX
。然后从handleDelete
触发对父对象的回调并在其中设置状态。示例代码如下。
Display.jsx
import {DisplayList} from './DisplayList';
class Display extends Component {
constructor(props){
let data = JSON.parse(localStorage.getItem('data'));
super(props)
this.state = {
data: data,
}
// Methods
this.displayValues = this.displayValues.bind(this);
}
displayValues(){
return this.state.data.map((data1, index) =>
<DisplayList
key = {index}
email = {data1.email}
password = {data1.password}
updateList = {this.updateList}
/>
)
}
// This is the method that will be called from the child component.
updateList = (data) => {
this.setState({
data
});
}
render() {
return (
<ul className="list-group">
{this.displayValues()}
</ul>
)
}
}
DisplayList.jsx
import {Button} from 'react-bootstrap';
export class DisplayList extends Component {
constructor(props){
super(props)
// Methods
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(){
const data = JSON.parse(localStorage.getItem('data'));
for (let index = 0; index < data.length; index++) {
if(this.props.email === data[index].email &&
this.props.password === data[index].password){
data.splice(data[index], 1);
}
}
localStorage.setItem('data', JSON.stringify(data));
this.props.updateList(data);
}
render() {
return (
<div className = "mt-4">
<li className="list-group-item text-justify">
Email: {this.props.email}
<br />
Password: {this.props.password}
<br />
<Button variant = "info mr-4 mt-1">Edit</Button>
<Button onClick = {this.handleDelete} variant = "danger mt-1">Delete</Button>
</li>
</div>
)
}
}
答案 1 :(得分:1)
您可以喜欢接受的答案,也可以使用其他方式来做到这一点,如下所示:
将handleDelete
本身的Parent
事件移动到了状态data
所在的地方。
class Display extends Component {
constructor(props){
let data = JSON.parse(localStorage.getItem('data'));
super(props)
this.state = {
data: data || [{email: 'j', password: "dsv" }],
}
}
handleDelete(email, password){
const data = JSON.parse(localStorage.getItem('data'));
data = data ? data : this.state.data
for (let index = 0; index < data.length; index++) {
if(email === data[index].email &&
password === data[index].password){
data.splice(data[index], 1);
}
}
localStorage.setItem('data', JSON.stringify(data));
this.setState({data});
}
render() {
console.log(this.state)
debugger
return (
<ul className="list-group">
{this.state.data.map((data1, index) =>
<DisplayList
key = {index}
email = {data1.email}
password = {data1.password}
handleDelete = {() => this.handleDelete(data1.email,data1.password )}
/>
)}
</ul>
)
}
}
和DisplayList
export default class DisplayList extends Component {
constructor(props){
super(props)
}
render() {
return (
<div className = "mt-4">
<li className="list-group-item text-justify">
Email: {this.props.email}
<br />
Password: {this.props.password}
<br />
<Button variant = "info mr-4 mt-1">Edit</Button>
<Button onClick = {this.props.handleDelete} variant = "danger mt-1">Delete</Button>
</li>
</div>
)
}
}