gpt4 book ai didi

javascript - 通过只传递一次 props 来多次执行状态和数组操作

转载 作者:行者123 更新时间:2023-11-30 14:20:48 28 4
gpt4 key购买 nike

我是 React 的新手,这件事让我很困惑。我有一个包含数组的根组件,我正在将函数 ADD 和 DELETE 作为 Prop 传递给子组件 ui_forms。在子组件中,我获取输入参数并根据按下的按钮从数组中推送或删除。但是,我似乎不能多次执行推送/删除操作,因为我想我只在根组件中注入(inject)了一次子组件。

有没有办法通过只发送一次 props 来执行用户想要的多次推送或删除?

谢谢

App.js

import FormsMap from './form_map';

class App extends Component {
state = {
maps : [
{'regno' : 'Karan', 'id' : 1},
{regno : 'Sahil', 'id' : 2},
{'regno' : 'Rahul', id : 4},
{regno : 'Mohit', id : 5},
{regno : 'Kartik', id : 3}
]
};

function_as_props(list1) {
console.log(this.state.maps);
let ninja = [...this.state.maps];
console.log(ninja);
ninja.push({"regno" : list1, "id" : Math.random()*10});

this.setState({
maps : ninja
});
console.log(ninja);


function_as_props1(name) {
let t = this.state.maps.indexOf(name);
let x = this.state.maps.splice(t,1);
console.log(x);
this.setState({
maps : x
});
console.log(this.state.maps);
}
}

render() {
const p = this.state.maps.map(list => {
return(
<div key={list.id}> {list.regno} </div>
);
})

return(
<FormsMap transpose = {this.function_as_props.bind(this)} traverse ={this.function_as_props1.bind(this)} /> <br />
);


}
}

export default app;

form_map.js

import React, { Component } from 'react';

class FormsMap extends Component {
state = {
name : null,
age : null,
hobby : null
};

changes(e) {
this.setState({
[e.target.id] : e.target.value
});
}

handle = (e) => {
e.preventDefault();
console.log(this.state);
this.props.transpose(this.state.name);
}

dels = (e) => {
this.setState({
[e.target.id] : e.target.value
});
}

del_button(e) {
e.preventDefault();
//console.log(this.state.name);
this.props.traverse(this.state.name);
}
render() {
return(
<React.Fragment>
<form onSubmit={this.handle}> {/* After entering all info, Press Enter*/}
<label htmlFor="labels"> Name : </label>
<input type="text" id="name" placeholder="Your name goes here..." onChange={this.changes.bind(this)} />
<label htmlFor="labels"> Age : </label>
<input type="text" id="age" placeholder="Your age goes here..." onChange={this.changes.bind(this)} />
<label htmlFor="labels"> Hobby : </label>
<input type="text" id="hobby" placeholder="Your hobby goes here..." onChange={this.changes.bind(this)} /> <br /><br />
<input type="submit" value="SUBMIT" /><br /><br />
</form>

<input type="text" id="name" placeholder="Enter name to delete..." onChange={this.dels} /> <button onClick={this.del_button.bind(this)}> DELETE </button>
</React.Fragment>

);
}
}

export default FormsMap;

最佳答案

试试这个


App.js

import React, { Component } from "react";
import FormsMap from "./components/FormsMap";

class App extends Component {
constructor(props) {
super(props);
this.state = {
maps: [
{ regno: "Karan", id: 1 },
{ regno: "Sahil", id: 2 },
{ regno: "Rahul", id: 4 },
{ regno: "Mohit", id: 5 },
{ regno: "Kartik", id: 3 }
]
};
}
function_as_props(list1) {
let ninja = this.state.maps.concat({
regno: list1,
id: Math.random() * 10
});
this.setState({ maps: ninja });
}
function_as_props1(name) {
let x = this.state.maps.filter(
list => list.regno.toLocaleLowerCase() !== name.toLocaleLowerCase()
);
this.setState({
maps: x
});
}
render() {
return (
<React.Fragment>
{this.state.maps.map(list => <div key={list.id}>{list.regno}</div>)}
<FormsMap
transpose={this.function_as_props.bind(this)}
traverse={this.function_as_props1.bind(this)}
/>
</React.Fragment>
);
}
}

export default App;

FormsMap.js

import React, { Component } from "react";

class FormsMap extends Component {
state = {
name: null,
age: null,
hobby: null
};

changes(e) {
this.setState({
[e.target.id]: e.target.value
});
}

handle = e => {
e.preventDefault();
this.props.transpose(this.state.name);
};

dels = e => {
this.setState({
[e.target.id]: e.target.value
});
};

del_button(e) {
e.preventDefault();
this.props.traverse(this.state.name);
}
render() {
return (
<React.Fragment>
<form onSubmit={this.handle}>
{" "}
{/* After entering all info, Press Enter*/}
<label htmlFor="labels"> Name : </label>
<input
type="text"
id="name"
placeholder="Your name goes here..."
onChange={this.changes.bind(this)}
/>
<label htmlFor="labels"> Age : </label>
<input
type="text"
id="age"
placeholder="Your age goes here..."
onChange={this.changes.bind(this)}
/>
<label htmlFor="labels"> Hobby : </label>
<input
type="text"
id="hobby"
placeholder="Your hobby goes here..."
onChange={this.changes.bind(this)}
/>{" "}
<br />
<br />
<input type="submit" value="SUBMIT" />
<br />
<br />
</form>
<input
type="text"
id="name"
placeholder="Enter name to delete..."
onChange={this.dels}
/>{" "}
<button onClick={this.del_button.bind(this)}> DELETE </button>
</React.Fragment>
);
}
}

export default FormsMap;

这是演示:https://codesandbox.io/s/xl97xm6zpo

关于javascript - 通过只传递一次 props 来多次执行状态和数组操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52782765/

28 4 0
文章推荐: c - 共享内存: SendMessage from VB6 to third-party DLL,从传递的结构中检索数据?
文章推荐: javascript - 通过对象的键对数据集进行分组并生成数组
文章推荐: javascript - Angular JS Promise 所有顺序
文章推荐: javascript - 如何根据字符数缩放
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com