简体   繁体   中英

How can I display the newly added item on top of the previous item?

How can I display the newly added item on top of the previous item?

<div className="App">
      <h2>{this.state.title}</h2>
      <form ref="myForm" className="myForm">
        <input type="text" ref="input" placeholder="your name" className="formField" />
        <button onClick={(e)=>this.fSubmit(e)} className="myButton">submit </button>
      </form>
      <pre>
      {datas.map((data, i) =>
          <li reverse key={i} className="myList">
            {data.input}
            <button onClick={()=>this.fRemove(i)} className="myListButton">remove </button>
            <button onClick={()=>this.fEdit(i)} className="myListButton">edit </button>
          </li>
      )}
    </pre>
    </div>

Just change this code

 <pre>
      {datas.map((data, i) =>
          <li reverse key={i} className="myList">
            {data.input}
            <button onClick={()=>this.fRemove(i)} className="myListButton">remove </button>
            <button onClick={()=>this.fEdit(i)} className="myListButton">edit </button>
          </li>
      )}
    </pre>

To this

 <pre className="reverse-list">
      {datas.map((data, i) =>
          <li key={i} className="myList">
            {data.input}
            <button onClick={()=>this.fRemove(i)} className="myListButton">remove </button>
            <button onClick={()=>this.fEdit(i)} className="myListButton">edit </button>
          </li>
      )}
    </pre>

CSS

.reverse-list{
  display:flex;
  flex-direction: column-reverse;
}

Note: reverse property only works on Ordered-list ol tag and the reversed attribute of the ol tag is not supported in Edge prior version 79.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM