Unable to set value for React MUI TextField in React Hook Form Controller inside MUI Stepper Dialog

I am facing an issue with my Button that triggers a MUI Dialog to open.
Within the Dialog, there is a MUI Stepper as part of my form which has different required and non-required inputs.

//Sample Input
<Controller
    name="stateName"
    control={control}
    rules={{ required: true }}
    render={({ field: { onChange, value } }) => (
      <TextField
        required
        label="stateName"
        variant="standard"
        onChange={onChange}
        value={value}
        fullWidth
        error={errors.stateName ? true : false}
        helperText={errors.stateName ? "Required field" : null}
      />
    )}
  />

For the entire example, refer to: https://codesandbox.io/s/gracious-tdd-dkzoqy

After submitting the form, I want to add the data to a list and display it with an edit button.
Upon clicking the edit button, I aim to populate the dialog's inputs with the edited data values.

I have tried using react-hook-form

setValue("field", value)
without success.
I also attempted passing the edit object through Props to nested form steps and using setValue within these components' useEffect utilizing useFormContext() but encountered issues.

How can I effectively pass the values to the inputs for correct display in the Multi-Step-Form-Dialog?

Answer №1

CSB Working Properly -> Check it out here!

If you encounter issues in the editHandler function, make sure to parse the state from a JSON string into an object:

const editHandler = (stateJSON: any) => {
    const state = JSON.parse(stateJSON);
    methods.reset(state);

For the submitHandler function, remember that the data is stringified. Here's how it should look:

const submitHandler = (data: any) => {
    setContent(prevContent => [...prevContent,data]);
    methods.reset();
    setEditState(undefined);
    setOpen(false);
};

Don't forget to refer to the React documentation on updating objects in state

and check out this resource on how to avoid mutations: Prevent Mutation Guide

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

Tips for creating a MaterialUI list that wraps to the next line?

import React from 'react'; import { List, ListItem, } from '@material-ui/core'; import { makeStyles, createStyles, } from '@material-ui/core/styles'; import clsx from 'clsx'; import VideoCard from './VideoCa ...

Utilizing React JSX within HTML structures

Attempting to integrate React into a basic HTML file, I came across an official guide on this website. However, I am struggling to grasp how to render a JSX-encoded component and how to nest one component within another: class LikeButton extends React.Co ...

Developing the headers for a service using React.js

As someone new to ReactJs, I'm curious about the various methods we can use to include Headers in our service Url before making a call. While I'm familiar with how GET/POST Calls are made in angular Js after including headers, I'd like to l ...

What is the process to assign a value received from the server to an Input field and then update

I am working on an input field that should initially display a value from the server const [nameValue, setNameValue] = useState(""); <TextField id="outlined-read-only-input" label="Display Nam ...

Convert the pager produced by ReactiveList into another language

In my React application, I am using ReactiveList (v2.17) to display a list with a pager at the bottom. The problem is that the pager buttons are in English ("Prev", "Next"), while the site is supposed to be in Dutch. Is there a way to translate these but ...

Expanding MaterialUi styled components by incorporating TableCellProps: A guide

When trying to create a styled TableCell component in a separate file, I encountered an error in TypeScript (ts(2322)). TypeScript was indicating that the properties "component," "scope," and "colSpan" could not be used because they do not exist in StyledC ...

Title bar / Popup within the confines of a child container

My dilemma arises from my desire to showcase a mobile phone replica on the webpage using components from Material UI, such as AppBar and Dialog. However, these components do not remain within the confines of the phone container; instead, they extend to the ...

Setting up Typescript with React 17 and Tailwind CSS version 2.0

I'm struggling to set up TailwindCSS 2.0 with the create-react-app --template typescript configuration and encountering errors. Even after following the instructions on the official TailwindCSS website for React at https://tailwindcss.com/docs/guides/ ...

A guide on toggling the password input type between text and hidden in a React application

I am facing an issue with toggling the input type between password and text to display the password in a login form. The expected functionality is that clicking on the eye icon next to the password input should reveal the password in plain text, but this i ...

State updates are not working correctly in React/Redux and it is not suitable for use in components

Exploring the realm of React and Redux, I have diligently followed the official Redux docs Todo List example at https://redux.js.org/basics/example. Recently, I decided to revamp my code structure by adopting the presentational/container components methodo ...

Quick tutorial on extracting an array of objects in Node.js

I need assistance with creating a simple todo app using React JS and Node. How can I fetch an object array (task list) from Node to React in order to display that list when a button is clicked? const lists =[ {toDo: "learn react"} ] app.post(&apos ...

Is it possible to stay on the current page even after a refresh in React Router?

Hello, I am new to using React and encountering an issue with React Router. I am currently navigating through different pages using useNavigate in react-router-dom, and everything is functioning correctly. However, I face a problem when I refresh the page. ...

Deploying React.js: Should you release the client and server separately or as a cohesive unit?

I currently have a react.js website with a backend in Node/express that is being hosted on Azure. The project is currently set up as monolithic, meaning every time I make a change to either the front-end or back-end, everything has to be redeployed in pr ...

Utilize Material UI to iterate through an array using a map function

Is there a specific way to handle mapping over objects from an axios call when using a Material UI table? const tableRows = this.props.reduxState.map(row => { const {item_description, purchase_date, item_price, item_link} ={TableRowColumn} ...

Error occurred while attempting to upload a file using multipart form data: TypeError message displayed - Unable to access properties of undefined (specifically '0')

I am encountering an issue while trying to send multipart/form-data using axios from the frontend. The same post request works fine in Postman/Insomnia but fails when executed from the frontend. The backend shows the following error: node:events:505 ...

Transform the appearance of a button when focused by utilizing CSS exclusively or altering it dynamically

As a newcomer to coding, I am currently working on a project that involves changing the color of buttons when they are clicked. Specifically, I want it so that when one button is clicked, its color changes, and when another button next to it is clicked, th ...

Exploring the functionality of multiple checkboxes in Next.js 14, the zod library, shadcn/ui components, and react-hook

I'm currently working on a form for a client where one of the questions requires the user to select checkboxes (or multiple checkboxes). I'm still learning about zod's schema so I'm facing some challenges in implementing this feature. I ...

The retrieved information can be seen in the Network tab, however, there seems to be an issue in the console

I am successfully fetching data from the server, but I can't seem to display it in console.log. The error message I'm encountering is shown below. I have confirmed that the data is being returned as an array. What steps should I take to ensure th ...

Submitting multiple values in React-PDF

I've been working on a form that allows users to input their name and surname, and then generate a pdf document from the entered information. To achieve this, I'm utilizing . However, I've encountered an issue where I can only submit one va ...

keen-slider may encounter compatibility issues with next.js

I have been attempting to transform the code shown in the screenshot below into a slider format using keen-slider. https://i.stack.imgur.com/M62iz.png <div className="card-con mt-6"> {!loadingImages && ...