ReactJS-提交表格后如何自动刷新
当我提交页面永远不会停止加载时,我必须手动刷新页面以获取表中的输入。 table.js和subm.js分为两个不同的组件。因此,如果我可以在表组件中添加SOM EventListner以在提交后再次获取API,该如何使我的页面自动刷新替代。
类提交扩展组件{
render() {
return (
<>
<form action='http://localhost:3001/api/post' method="POST" onsubmit="return false">
<input type="input" class="text" name="brand" placeholder="Brand" required />
<input type="input" class="text" placeholder="Model" required />
<input type="input" class="text" name="price" placeholder="Price" required />
<button type="submit" class='button'>Submit</button>
</form>
</>
)
}
}
function table(){
useEffect(() => {
Axios.get("http://localhost:3001/api/get/carmodels").then((response) => {
setCarModelList(response.data)
})
}, [])
const [carModelList, setCarModelList] = useState([])
const DeleteCar = (val) => {
const next = [...carModelList];
const removedItems = next.splice(next.indexOf(val), 1);
const deleteCarModel = Axios.delete(`http://localhost:3001/api/delete/${val.id}`);
setCarModelList(next);
return deleteCarModel
}
const renderTableData = () => {
return carModelList.map((val) => (
<tr class>
<td>{val.id}</td>
<td>{val.brand}</td>
<td>{val.model}</td>
<td>{val.price}</td>
<td>
<button id="delete" onClick={() => DeleteCar(val)}>Delete</button>
</td>
</tr>))
}
return (
<table id="table">
<thead>
<tr>
<th>Id</th>
<th>Brand</th>
<th>Model</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{renderTableData()}
</tbody>
</table>
);
}
When i submit the page never stops loading and i have to manually refresh the page to get the input in my table. the table.js and submit.js is in two different components. So how do i make my page automatically refresh alternativly if i could add som eventlistner in the table component to get the API again after a submit.
class Submit extends Component {
render() {
return (
<>
<form action='http://localhost:3001/api/post' method="POST" onsubmit="return false">
<input type="input" class="text" name="brand" placeholder="Brand" required />
<input type="input" class="text" placeholder="Model" required />
<input type="input" class="text" name="price" placeholder="Price" required />
<button type="submit" class='button'>Submit</button>
</form>
</>
)
}
}
function Table() {
useEffect(() => {
Axios.get("http://localhost:3001/api/get/carmodels").then((response) => {
setCarModelList(response.data)
})
}, [])
const [carModelList, setCarModelList] = useState([])
const DeleteCar = (val) => {
const next = [...carModelList];
const removedItems = next.splice(next.indexOf(val), 1);
const deleteCarModel = Axios.delete(`http://localhost:3001/api/delete/${val.id}`);
setCarModelList(next);
return deleteCarModel
}
const renderTableData = () => {
return carModelList.map((val) => (
<tr class>
<td>{val.id}</td>
<td>{val.brand}</td>
<td>{val.model}</td>
<td>{val.price}</td>
<td>
<button id="delete" onClick={() => DeleteCar(val)}>Delete</button>
</td>
</tr>))
}
return (
<table id="table">
<thead>
<tr>
<th>Id</th>
<th>Brand</th>
<th>Model</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{renderTableData()}
</tbody>
</table>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码示例为另一个在单击提交按钮时初始化为 false 的警报变量添加 UseState ,你将其设为 true,并在获取后将其恢复为 false。附图中的示例
code exampleAdd UseState for another alert variable that is initialized to false, when you click the submit button, you make it true, and after you fetch, you turn it back to false. example in the image attached