单击提交按钮后如何重置输入值? [英] How to reset input value after clicking submit button?
问题描述
import React, { useState } from 'react';
import './StockQuotes.css';
import { createMuiTheme, withStyles, makeStyles, ThemeProvider } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import stockData from '../util/stockData';
import { useDispatch } from 'react-redux';
import { getStocksOwned } from '../slices/stocksOwnedSlice';
const StockQuotes = () => {
const [sharesToBuy, setSharesToBuy] = useState(stockData);
const dispatch = useDispatch();
const handleChange = (event, index) => {
stockData[index].owned = parseInt(event.target.value);
setSharesToBuy(stockData);
}
const handleClick = (event, index) => {
event.preventDefault();
dispatch(getStocksOwned(sharesToBuy));
stockData[index].owned = 0;
}
const StyledTableCell = withStyles((theme) => ({
head: {
backgroundColor: theme.palette.common.black,
color: theme.palette.common.white,
},
body: {
fontSize: 14,
},
}))(TableCell);
const StyledTableRow = withStyles((theme) => ({
root: {
'&:nth-of-type(odd)': {
backgroundColor: theme.palette.action.hover,
},
},
}))(TableRow);
const useStyles = makeStyles((theme) => ({
margin: {
margin: theme.spacing(1),
},
table: {
minWidth: 700,
},
}));
const classes = useStyles();
const theme = createMuiTheme({
palette: {
primary: {main: '#00e676'},
},
});
return(
<TableContainer component={Paper}>
<Table className={classes.table} aria-label="customized table">
<TableHead>
<TableRow>
<StyledTableCell>Stock Name</StyledTableCell>
<StyledTableCell align="right">Current Price</StyledTableCell>
<StyledTableCell align="right">Shares</StyledTableCell>
<StyledTableCell align="right">Order</StyledTableCell>
</TableRow>
</TableHead>
<TableBody>
{stockData.map((stock, index) => (
<StyledTableRow key = {index} >
<StyledTableCell component="th" scope="row">
{stock.name}
</StyledTableCell>
<StyledTableCell align="right">${stock.price}</StyledTableCell>
<StyledTableCell align="right"><input type="number" onChange={event => handleChange(event, index)}></input></StyledTableCell>
<StyledTableCell align="right">
<ThemeProvider theme={theme}>
<Button variant="contained" color="primary" className={classes.margin} onClick={event => handleClick(event, index)}>
BUY
</Button>
</ThemeProvider>
</StyledTableCell>
</StyledTableRow>
))}
</TableBody>
</Table>
</TableContainer>
)
}
export default StockQuotes;
单击提交按钮后,我试图将 stockData[index].owned 设置为 0.通常是直截了当的,但这次通过映射包含 8 个对象的 stockData json 文件创建了 8 个输入字段.因此,如果我按输入标签放置 value 属性然后键入,则整个 8 个输入字段都会更改.所以我以这种方式编码,除了在我点击后,我得到TypeError:无法分配给对象#"的只读属性拥有"". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .我能做什么?
I'm trying to get stockData[index].owned to 0 after I click submit button. Usually straight forward, but this time 8 input fields are created through mapping stockData json file which contains 8 objects. So if I put value property by input tag then type, entire 8 input field changes. So I coded this way except after I click, I get "TypeError: Cannot assign to read only property 'owned' of object '#'". What can I do?
推荐答案
这里发生了一些事情:
stockData
似乎只是用于设置组件状态的初始值.初始化状态后,更新并从sharesToBuy
读取,以便这些更改流向您的组件树.- 不要改变对象/状态.这可能会导致错误并阻止您的组件正确更新.而是使用更新的值创建一个新副本.
- 控制您的
input
并根据状态变化更新value
.
stockData
appears to just be an initial value for setting the component state. Once state is initialized, update and read fromsharesToBuy
so those changes flow to your component tree.- Don't mutate objects/state. That can cause bugs and prevent your component from updating properly. Instead, create a new copy with updated values.
- Make your
input
controlled and update thevalue
based on state changes.
// Convenience function for updating state.
const updateOwnedValue = (index, owned) => {
// Instead of mutating the array/object, create a copy with the updated values
// and then update state so the changes flow down to your components.
const newState = sharesToBuy.map((stock, idx) => (
idx === index ? { ...stock, owned } : stock
));
setSharesToBuy(newState);
};
const handleChange = (event, index) => {
updateOwnedValue(index, parseInt(event.target.value));
}
const handleClick = (event, index) => {
event.preventDefault();
dispatch(getStocksOwned(sharesToBuy));
updateOwnedValue(index, 0);
}
...
// Iterate over component state instead of the imported value.
{sharesToBuy.map((stock, index) => (
...
<input
type="number"
onChange={event => handleChange(event, index)}
value={
// This makes your input a controlled component.
// The value will respond to state changes.
stock.owned
}
/>
...
))}
这篇关于单击提交按钮后如何重置输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!