简体   繁体   English

输入一个数字后,React 输入字段失去焦点

[英]React input field lose focus after typing one number

Currently, as soon as I type a number, input field lose focus.目前,只要我输入一个数字,输入字段就会失去焦点。 So I took the sharesToBuy.map portion then assign it to variable then moved it outside StockQuotes component.所以我拿了sharesToBuy.map 部分,然后将它分配给变量,然后将它移到StockQuotes 组件之外。 Then I get error msg saying StyleTabledCell and StyleTable row not recogonized from material ui.然后我收到错误消息,说 StyleTabledCell 和 StyleTable 行无法从材料 ui 中识别出来。 How can I re-arrange my component so input field don't lose focus?如何重新排列我的组件以便输入字段不会失去焦点? Or is there any hook that I can use to solve this issue?或者有什么钩子可以用来解决这个问题? I tried autofocus(), useRef and moving component outside, but nothing seems working.我尝试了 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>
    )
}


Issue问题

You are defining your styled components within the render function of your StockQuotes component.您正在StockQuotes组件的渲染函数中定义样式组件。

The entire function body of a functional component is the render function!函数式组件的整个函数体就是渲染函数! The withStyles HOC will create a new component each time if within the render.如果在渲染中, withStyles HOC 每次都会创建一个新组件。

Solution解决方案

Simply move StyledTableCell and StyledTableRow outside of StockQuotes component.只需将StyledTableCellStyledTableRow移到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>
  );
};

编辑 react-input-field-lose-focus-after-type-one-number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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