React中提交表单后如何将录入信息按姓氏排序展示到表格
功能需求梳理
- 页面顶部配置可输入表单,支持用户录入名(First name)、姓(Last name)、手机号三类信息,搭配提交按钮完成条目新增
- 提交操作触发后,本次录入的信息需与所有历史录入的通讯录条目合并,按姓氏自动升序排序后,完整展示在页面下方的表格模块中
现有代码待补全的逻辑缺口
- 表单输入项为硬编码静态值,未绑定React受控状态,无法实时获取用户输入的实际内容
- 表单提交逻辑仅拦截了默认页面跳转行为,未完成录入值的向上传递、持久化存储流程
- 顶层
Application组件未维护通讯录列表的全局状态,没有打通子组件间的数据传递通路 - 表格组件仅实现了表头结构渲染,缺少对通讯录条目的循环渲染逻辑
修正后可直接运行的完整代码
import React, { useState } from 'react'; import ReactDOM from 'react-dom'; const style = { table: { borderCollapse: 'collapse' }, tableCell: { border: '1px solid gray', margin: 0, padding: '5px 10px', width: 'max-content', minWidth: '150px' }, form: { container: { padding: '20px', border: '1px solid #F0F8FF', borderRadius: '15px', width: 'max-content', marginBottom: '40px' }, inputs: { marginBottom: '5px' }, submitBtn: { marginTop: '10px', padding: '10px 15px', border:'none', backgroundColor: 'lightseagreen', fontSize: '14px', borderRadius: '5px' } } } function PhoneBookForm({ addEntryToPhoneBook }) { // 绑定表单输入的受控状态,保留原代码的默认填充值 const [firstName, setFirstName] = useState('Coder'); const [lastName, setLastName] = useState('Byte'); const [phone, setPhone] = useState('8885559999'); const handleFormSubmit = (e) => { e.preventDefault(); // 调用上层回调,传递当前录入的条目信息 addEntryToPhoneBook({ firstName: firstName.trim(), lastName: lastName.trim(), phone: phone.trim() }) // 提交完成后清空表单输入 setFirstName(''); setLastName(''); setPhone(''); } return ( <form onSubmit={handleFormSubmit} style={style.form.container}> <label>First name:</label> <br /> <input style={style.form.inputs} className='userFirstname' name='userFirstname' type='text' value={firstName} onChange={(e) => setFirstName(e.target.value)} /> <br/> <label>Last name:</label> <br /> <input style={style.form.inputs} className='userLastname' name='userLastname' type='text' value={lastName} onChange={(e) => setLastName(e.target.value)} /> <br/> <label>Phone:</label> <br /> <input style={style.form.inputs} className='userPhone' name='userPhone' type='text' value={phone} onChange={(e) => setPhone(e.target.value)} /> <br/> <input style={style.form.submitBtn} className='submitButton' type='submit' value='Add User' /> </form> ) } function InformationTable({ contacts }) { return ( <table style={style.table} className='informationTable'> <thead> <tr> <th style={style.tableCell}>First name</th> <th style={style.tableCell}>Last name</th> <th style={style.tableCell}>Phone</th> </tr> </thead> <tbody> {contacts.map((contact, index) => ( <tr key={index}> <td style={style.tableCell}>{contact.firstName}</td> <td style={style.tableCell}>{contact.lastName}</td> <td style={style.tableCell}>{contact.phone}</td> </tr> ))} </tbody> </table> ); } function Application(props) { // 顶层维护通讯录列表的全局状态 const [contactList, setContactList] = useState([]); const addNewContact = (newContact) => { // 合并新旧条目,按姓氏字母顺序升序排序 const sortedList = [...contactList, newContact].sort((a, b) => { return a.lastName.localeCompare(b.lastName); }); setContactList(sortedList); } return ( <section> <PhoneBookForm addEntryToPhoneBook={addNewContact} /> <InformationTable contacts={contactList} /> </section> ); } ReactDOM.render( <Application />, document.getElementById('root') );
内容的提问来源于stack exchange,提问作者user10579548
相关产品推荐
相关产品推荐

