亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否可以使此刪除按鈕和輸入表單起作用?

是否可以使此刪除按鈕和輸入表單起作用?

慕娘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} />


查看完整回答
反對 回復 2022-12-09
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號