React TypeError:无法读取未定义的属性(读取“状态”)

萨加尔

当我单击该按钮时,我想更新该按钮的文本。我正在使用setState它,但我无法使用它,因为它给了我以下错误:

TypeError: Cannot read properties of undefined (reading 'state')

这是我的代码:

import Head from 'next/head';
import { Box, Container, Grid, Pagination } from '@mui/material';
import { products } from '../__mocks__/products';
import { ProductListToolbar } from '../components/product/product-list-toolbar';
import { ProductCard } from '../components/product/product-card';
import { DashboardLayout } from '../components/dashboard-layout';
import { CustomerListResults } from '../components/trip/trip-list-results';
import { customers } from '../__mocks__/customers';
import { trips } from '../__mocks__/trips';
import { TripListResults } from '../components/customer/customer-list-results';
import {
  Avatar,
  Card,
  Checkbox,
  Table,
  TableBody,
  TableCell,
  TableHead,
  TablePagination,
  TableRow,
  Typography,
  Button
} from '@mui/material';
import NextLink from 'next/link';

const TripRequests = () => {
    
    const accept =()=> {
        this.setState({accept: 'Payment Pending'})
        console.log("fsfdsa");
    };
    this.state = {
        accept: "Accept"
    };
    
    return (
  <>
    <Head>
      <title>
        Trip Requests
      </title>
    </Head>
    <Box
      component="main"
      sx={{
        flexGrow: 1,
        py: 8
      }}
    >
      <Container maxWidth={false}>
        <Box sx={{ mt: 3 }}>
          {/* <CustomerListResults customers={trips} /> */}
          <h2>Trip Requests (2)</h2>
        </Box>
        <Box sx={{ minWidth: 1050, mt: 3 }}>
          <Table>
            <TableHead>
              <TableRow>
                <TableCell padding="checkbox">
                  {/* <Checkbox
                    // checked={selectedCustomerIds.length === customers.length}
                    color="primary"
                    // indeterminate={
                    //   selectedCustomerIds.length > 0
                    //   && selectedCustomerIds.length < customers.length
                    // }
                    // onChange={handleSelectAll}
                  /> */}
                </TableCell>
                {/* <TableCell>
                  Trip Id
                </TableCell> */}
                <TableCell>
                  Customer
                </TableCell>
                <TableCell>
                  Departure
                </TableCell>
                <TableCell>
                  Destination
                </TableCell>
                <TableCell>
                Truck / Driver
                </TableCell>
                <TableCell>
                Action
                </TableCell>
              </TableRow>
            </TableHead>
            <TableBody>
             
                <TableRow
                
                  hover
                //   key={customer.id}
                //   selected={selectedCustomerIds.indexOf(customer.id) !== -1}
                >
                  <TableCell padding="checkbox">
                    {/* <Checkbox
                      checked={selectedCustomerIds.indexOf(customer.id) !== -1}
                      onChange={(event) => handleSelectOne(event, customer.id)}
                      value="true"
                    /> */}
                  </TableCell>
                  <TableCell>
                    Rohan Joshi 
                  </TableCell>
                  <TableCell>
                    <Box
                      sx={{
                        alignItems: 'center',
                        display: 'flex'
                      }}
                    >
                      {/* <Avatar
                        src={customer.avatarUrl}
                        sx={{ mr: 2 }}
                      >
                        {getInitials(customer.name)}
                      </Avatar> */}
                      <Typography
                        color="textPrimary"
                        variant="body1"
                      >
                        A-50, Sec 67, Noida
                      </Typography>
                    </Box>
                  </TableCell>
                  
                  <TableCell>
                   HUDA City Center
                  </TableCell>
                  
                  <TableCell>
                    fds
                  </TableCell>
                  <TableCell>
                    <Button onClick={accept}>{state.accept}</Button>
                    <Button>Decline</Button>

                  </TableCell>
                 
                  {/* <TableCell>
                    {format(customer.createdAt, 'dd/MM/yyyy')}
                  </TableCell> */}
                </TableRow>

            </TableBody>
          </Table>
        </Box>
        
      </Container>
    </Box>
  </>
);

TripRequests.getLayout = (page) => (
  <DashboardLayout>
    {page}
  </DashboardLayout>
)};

export default TripRequests;

菲利克斯·克林

看起来您正在将基于类的组件与功能组件混合在一起。在您的情况下this,指的是模块级别 this,即undefined. 函数组件不使用this.

要在函数组件中使用状态,您需要使用useState钩子

const TripRequests = () => {
  const [acceptState, setAcceptState] = useState('Accept')
 
  const accept = () => setAcceptState('Payment Pending')

  // ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React教程:TypeError:无法读取未定义的属性“ props”

React + Fetch + Json。TypeError:无法读取未定义的属性

MobX React TypeError:无法读取未定义的属性

TypeError:无法读取未定义React的属性'value'

TypeError:无法读取未定义的React Hooks的属性“ map”

REACT TypeError:无法读取未定义的属性“值”

TypeError:无法在react中读取未定义的属性'reduce'

TypeError:无法读取未定义的属性'then'-React Hooks

React Jest:TypeError:无法读取未定义的属性“ map”

TypeError:无法读取未定义的React Express的属性'prototype'

TypeError:无法读取未定义的属性“状态”。更新了React

Typescript,React:TypeError:无法读取未定义的属性“ push”

TypeError:无法在React中读取未定义的属性'很多对象中的状态名称'

React-TypeError:无法读取未定义的属性“ includes”

TypeError:无法读取未定义的React Native的属性'map'

React Native + Fetch => TypeError:无法读取未定义的属性“then”

× react TypeError:无法读取未定义的属性“then”

React TypeError:无法读取未定义的属性“绑定”

React - TypeError 无法读取未定义的属性“地图”

React - TypeError:无法读取未定义的属性“renderSidebaritem”

React TypeError:无法读取未定义问题的属性“categoryName”

React JSX 返回 TypeError:无法读取未定义的属性

React TypeError:无法读取未定义的属性“结果”

React js TypeError:无法读取未定义的属性“params”

React Redux TypeError:无法读取未定义的属性“标题”

React-TypeError:无法读取未定义的属性“img”

React TypeError:无法读取未定义的属性“0”

React Native:无法读取未定义的属性“状态”

React - TypeError:无法读取未定义的属性(读取“参数”)?