慕娘9325324
2022-12-09 15:25:21
所以 iv 現在被卡住了很長一段時間,我需要讓我的刪除按鈕在每一行上都起作用,并讓輸入將信息吐到我的表中。我的問題是:甚至有可能做到嗎?(我今天才開始從字面上學習react/js)。最終產品應該看起來像“待辦事項列表”,但當我打開它時,它會自動具有來自 api 的值,我可以刪除和添加新的值。我的代碼: export default class FetchRandomUser extends React.Component { state = { loading: true, people: [] }; async componentDidMount() { const url = "https://swapi.dev/api/people/"; const response = await fetch(url); const data = await response.json(); const pirmadata = data; this.setState({ people: pirmadata.results, loading: false }); console.log(pirmadata) } render() { if (this.state.loading) { return <div>loading...</div>; } if (!this.state.people.length) { return <div>didn't get a person</div>; } return ( <div className="listas"> {this.state.people.map(person => ( <tr className="taras" key={person.name + person.age}> <th className="name">{person.name}</th> <th className="birth">{person.birth_year}</th> <th className="genders">{person.gender}</th> <th><button type="submit" className="delete">Delete</button></th> </tr> ))} </div> ); }}我的表單是在不同的 .js 文件上制作的,基本上是一個包含此表單的函數:<div className="forma"> <form> <input type="text" placeholder="Enter name :"></input> <input type="text" placeholder="Enter birth date :"></input> <input type="text" placeholder="Enter gender :"></input> <button type="submit">Submit entry!</button> </form></div>
1 回答

白衣非少年
TA貢獻1155條經驗 獲得超0個贊
click為作為元素onSubmit處理程序的刪除按鈕添加一個處理程序form。這樣當您單擊它時,它將調用表單的處理函數。
將 th 元素作為 prop傳遞handlerFunc給組件,以便能夠按以下方式使用它:
function YourComponent({ handler }){
return <th><button onClick={handler} className="delete">Delete</button></th>);
}
表單 onSubmit 句柄:
<form onSubmit={handlerFunc}>
<input type="text" placeholder="Enter name :"></input>
<input type="text" placeholder="Enter birth date :"></input>
<input type="text" placeholder="Enter gender :"></input>
<button type="submit">Submit entry!</button>
</form>
<YourComponent handler={handlerFunc} />
添加回答
舉報
0/150
提交
取消