Updating TextField Values with React Material UI

At the beginning of each session, I initialize the state with these variables:

state = {
    firm: null,
    office: null,
    salesCode: null,
    account: null
}

Each TextField in my application is rendered like this:

<TableCell>
    <TextField
        id="standard-bare"
        defaultValue={items.data[i].firm}
        margin="normal"
        onChange={(e) => this.handleChange(e, items.data[i].id)}
    />
</TableCell>

I have a handleChange method to update the state based on user input:

handleChange({ event, id }) {
    const { data } = this.state;
    data[id] = event.target.value;
    this.setState({data})
}

However, when I try to edit the text inside a TextField, I encounter the following error:

TypeError: Cannot read property 'target' of undefined
UserDataTable.handleChange:75
  72 |    
  73 |    handleChange({ event, id }) {
  74 |        const { data } = this.state;
> 75 |        data[id] = event.target.value;
     | ^  76 |        this.setState({data})
  77 |    }
  78 | 

The goal is to implement an edit function for the table where users can update information and trigger API calls to reflect those changes.

Answer №1

Make sure to pass an object to the handleChange function for extraction, not just individual parameters

Update the code from:

handleChange({ event, id }) {

To:

 handleChange(event, id) {

Additionally, you are trying to set the input value to a data object by ID without defining any data in the state. This should be corrected as shown below:

     handleChange(event, id) {
        this.setState({
              [id]: event.target.value
        })
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The useSelector from @reduxjs/toolkit in Next.js is returning an undefined value

Utilizing js and @reduxjs/toolkit in my current project has resulted in an issue where the useSelector method is returning undefined values when trying to access data from the store. Below is a snippet of my reducer file: import { createSlice } from "@red ...

A guide on harnessing the power of getServerSideProps

I'm a beginner in Next.js and I've just started learning about pre-rendering. I'm a bit confused on how to update props data from getServerSideProps. _app.tsx import type { AppProps } from 'next/app'; function MyApp({ Component, ...

Dealing with an empty array in a Next.js React application

While working with React, I encountered an error message stating "error: type is not supported." It seems like this error occurs when one of the array items is empty and the script cannot read that value. Specifically, if blog.author is empty, the syntax u ...

Hovering over a row in a DataGrid to trigger an event

Can anyone provide some guidance on how to create an event triggered by hovering over a row in a DataGrid? I also need to be able to access the data stored in that specific row cell. It seems like I may need to develop a custom Row component and link it ...

When utilizing the data property within the useQuery() hook, an error may arise stating "Unable to

This problem has me scratching my head. The code snippet below is triggering this error: TypeError: Cannot read properties of undefined (reading 'map') When I use console.log() to check res.data, everything seems normal and there is data present ...

Using this.setState in ReactJS removes filters

Hey everyone, I've been struggling with a technical issue for the past few days and would really appreciate any hints or solutions. The problem lies in creating a table using the material-table library. Specifically, I need to extract the docID and do ...

Tips for wrapping text in a column within material-ui's DataGrid

Struggling to apply word wrap to my column header title in DataGrid from material-ui. I've attempted using sx and style with no success. I even tried this: const StyledDataGridtwo = styled(DataGrid)<DataGridProps>(({ theme }) => ({ root: { ...

What is the reason behind the required designation for the props onClose and onOpen in the SwipeableDrawer component of Material-UI?

It seems that the onOpen and onClose properties aim to incorporate elements of the Observer pattern. But why is it necessary for a client using the SwipeableDrawer component who does not wish to observe these events to still provide values for these prope ...

What is the best way to set breakpoints on Material UI components?

Currently, I am exploring material-ui and I'm curious if there is a way to set all breakpoints at once. The code snippet below seems quite repetitive. Is there a more efficient method to achieve this? <Grid xs={12} sm={12} md={12} lg={12} xl={12} ...

Caution when using a React form: Value of `true` has been detected for a non-boolean attribute `validate`

I am trying to address a warning message that I have received index.js:1 Warning: Received true for a non-boolean attribute validate. If you want to write it to the DOM, pass a string instead: validate="true" or validate={value.toString()}. I ...

The error message "Uncaught ReferenceError: process is not defined" indicates that

While attempting to execute my code, I encountered the following error in the React console: An uncaught ReferenceError: process is not defined I am seeking guidance on resolving this error. Any assistance would be greatly appreciated. ...

Setting Default Values for Multi-select in React-select Async

What is the method for setting default selected values in React-select Async Multi-select? Below is a sample form utilizing react-hook-form and react-select: <form onSubmit={handleSubmit(onSubmit)} > {updateError && renderError(updateError)} ...

ReactJS application encountering CORS error while accessing endpoint in Spring Boot Application secured with Keycloak

I am currently working on integrating ReactJS and Java Spring Boot applications, both protected by Keycloak 11.0.2. Keycloak is running on port 8083, ReactJS on port 3000, and the Spring Boot application on port 8085. However, when I attempt to utilize th ...

What is the best way to retrieve data (using GET) following React state changes?

Whenever a user clicks on one of the orderBy buttons (such as name/email/date), a new rendered result should be fetched from the server by sending a new get request. The same applies to page pagination. Simply setting this.setState({ [thestate]: [newState ...

Encountering an anomaly in persistent redux while using NEXT.JS

I recently made some updates to my ecommerce store, including adding login and register options. To ensure that user tokens are saved even after a page refresh, I decided to implement redux-persist. However, I encountered an issue where the design breaks a ...

How to eliminate the blue border from a Select box in React using Material UI

I'm currently working on a project that includes a React Material UI Select component. I've successfully applied custom styles to it, but I'm facing an issue with a persistent blue outline around the Select box. This outline appears when the ...

Navigational elements, drawers, and flexible designs in Material-UI

I'm working on implementing a rechart in a component, but I've encountered an issue related to a flex tag. This is causing some problems as I don't have enough knowledge about CSS to find a workaround. In my nav style, I have display: flex, ...

Refs cannot be assigned to function components in MUI v5 Autocomplete

When attempting to utilize a custom component for the ListboxComponent prop in Autocomplete MUI v5, an error is encountered: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? Autocom ...

Customize Material UI Button Text

I'm experiencing an issue with Material UI buttons where the text within them defaults to uppercase. I've been trying to override this behavior so that the text remains as I type it in, without being automatically converted to uppercase. Despite ...

Unable to utilize mobs decorator as an error is occurring: Experimental syntax 'decorators-legacy' is not enabled at the moment

Having successfully used mobx decorators in a React Native project, I now encounter an issue while developing a website with the React.Js library. The error message states: Support for the experimental syntax 'decorators-legacy' isn't curre ...