当前,我键入数字后,输入字段便失去焦点。因此,我使用了sharesToBuy.map部分,然后将其分配给变量,然后将其移至StockQuotes组件之外。然后我得到错误消息,说不是从材料ui重新分配StyleTabledCell和StyleTable行。如何重新排列组件,使输入字段不会失去焦点?还是可以使用任何挂钩来解决此问题?我尝试了autofocus(),useRef和将组件移到外部,但是似乎没有任何作用。
import React, { useState, useEffect } 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 = ({ availableFunds, setAvailableFunds }) => {
const [sharesToBuy, setSharesToBuy] = useState(stockData);
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>
{sharesToBuy.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" value ={stock.owned} onChange={event => handleChange(event, index)}></input></StyledTableCell>
<StyledTableCell align="right">
<ThemeProvider theme={theme}>
<Button variant="contained" color="primary" className={classes.margin}
disabled={stock.owned === 0 || !stock.owned}
onClick={event => handleClick(event, index)}>
BUY
</Button>
</ThemeProvider>
</StyledTableCell>
</StyledTableRow>
))}
</TableBody>
</Table>
</TableContainer>
)
}
答案 0 :(得分:1)
您正在StockQuotes
组件的render函数中定义样式化的组件。
功能组件 的 entire 功能主体是渲染功能!
withStyles
HOC每次都会在渲染中创建一个新组件。
只需将StyledTableCell
和StyledTableRow
移到StockQuotes
组件之外。
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 StockQuotes = ({ availableFunds, setAvailableFunds }) => {
...
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>
{sharesToBuy.map((stock, index) => (
<StyledTableRow key={stock.name}>
<StyledTableCell component="th" scope="row">
{stock.name}
</StyledTableCell>
<StyledTableCell align="right">${stock.price}</StyledTableCell>
<StyledTableCell align="right">
<input
type="number"
value={stock.owned}
onChange={(event) => handleChange(event, index)}
></input>
</StyledTableCell>
<StyledTableCell align="right">
<ThemeProvider theme={theme}>
<Button
variant="contained"
color="primary"
className={classes.margin}
disabled={stock.owned === 0 || !stock.owned}
onClick={(event) => handleClick(event, index)}
>
BUY
</Button>
</ThemeProvider>
</StyledTableCell>
</StyledTableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
};