What is the best way to pass the value from one textfield to another using material UI in a react application?

I'm looking to transfer the content from a text field in a dialog window to another text field that is not embedded. However, instead of transferring the actual text field value, I'm getting an output of "object Object". Can you help me figure out why it's not working?

Within a functional component, I have created two different states. The purpose of the function "setOriginalTextValue" is to move the value from one state to the other:

export default function TextTransfer() {
  const [value, setValue] = React.useState();
  const [dialogValue, setDialogValue] = React.useState();

  const [open, setOpen] = React.useState(false);

  const handleClickOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  const handleChange = (event) => {
    setValue({
      value: event.target.value,
    });
  };

  const handleDialogValueChange = (event) => {
    setDialogValue({
      dialogValue: event.target.value,
    });
  };

  const setOriginalTextValue = () => {
    setValue({
      value: dialogValue,
    });
  };

  return (
    <div>
      <Button variant="text" color="inherit">
        <TextField
          id="outlined-multiline-static"
          label="Question"
          multiline
          onClick={handleClickOpen}
          rows={4}
          value={value}
          placeholder="Type here"
          onChange={handleChange}
          variant="outlined"
          style={{
            backgroundColor: "white",
          }}
        />
      </Button>
      <Dialog
        open={open}
        onClose={handleClose}
        onExit={setOriginalTextValue}
        aria-labelledby="form-dialog-title"
      >
        <DialogTitle id="form-dialog-title">Dialog Title</DialogTitle>
        <DialogContent>
          <DialogContentText>Please enter some text below</DialogContentText>
          <TextField
            autoFocus
            margin="dense"
            id="name"
            label="Dialog Text"
            multiline
            rows={4}
            onChange={handleDialogValueChange}
            fullWidth
            onExit={console.log("the dialog value", dialogValue)}
          />
        </DialogContent>
        <DialogActions>
          <Button onClick={handleClose} color="primary">
            Cancel
          </Button>
          <Button onClick={handleClose} color="primary">
            Transfer
          </Button>
        </DialogActions>
      </Dialog>
    </div>
  );
}

Answer №1

It seems that you are assigning objects when the expected input is strings. To resolve this issue, make the following changes:

 const updateInputValue = (event) => {
    setInputValue(event.target.value);
};

const updateDialogContent = (event) => {
    setDialogContent(event.target.value);
};

const updateOriginalTextValue = () => {
    setInputValue(dialogContent);
};

The setters for useState should be provided with the direct value, as they are designed to work with the variable name specified in their declaration.

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

Unable to render HTML through Jquery ajax functionality

success: function(json) { for (var i = 0; i < json.length; i++) { var response = json[i]; $('#rv-container').html('<p>' + response.name + '</p>' + '<span>' + response ...

Guide to setting up an object array in JavaScript with unique child elements and no duplicate values

I have an array that looks like this: sampleArray = ["x", "y", "z". "a.b", "a.b.c", "a.b.d", "a.e.f", "a.e.g"] However, I am aiming to transform it into the following structure, or something similar to it: newArray: [ { "x": [] }, ...

The source 'http://localhost:3000' is not authorized to access the Twitter API

I am working on a web application that utilizes the Twitter API REST. On one of my pages, I have a list of Twitter accounts and a button to add a new account. When this button is clicked, it triggers a function in the Angular controller: // Function to ...

What is the process for importing a JSON5 file in Typescript, just like you would with a regular JSON file?

I am looking to import a JSON5 file into a JavaScript object similar to how one can import a JSON file using [import config from '../config.json']. When hovering over, this message is displayed but it's clearly visible. Cannot find module & ...

Having trouble displaying real-time camera RTSP streaming using Angular

I am currently in the process of developing a web application using Angular and I need to incorporate a window that displays live RTSP streaming. Upon conducting research, I discovered that this can be achieved by utilizing the JSMpeg JavaScript library. ...

What is the best way to retrieve the current value of a React useState hook within a setInterval function while using Highcharts

import Error from 'next/error' import React, { useState, useEffect } from 'react' import Highcharts from 'highcharts' import HighchartsReact from 'highcharts-react-official' function generateChart() { const [co ...

Is it possible to modify @page directive(CSS) values from the code-behind(C#) or JavaScript?

Using the @page directive, you can define the printer margins for a page separately from regular CSS margins: <style type="text/css" media="print"> @page { size: auto; /* auto is the current printer page size */ margin ...

It appears that my React component is not being rerendered even after using componentDidMount and setState to update it

/*I have successfully fetched data and set it to state, however, when attempting to generate an array of jsx items for display, the array appears empty and nothing is rendering. I tried hardcoding and it worked. I also logged the data which showed that th ...

Having trouble retrieving the NextAuth session data within Next.js 12 middleware

I've been working on implementing route protection using Next.js 12 middleware function, but I keep encountering an issue where every time I try to access the session, it returns null. This is preventing me from getting the expected results. Can anyon ...

Tips for showcasing a different value than the default renderValue in a Material UI select component

My dropdown menu is set up to display flags from 3 different countries, and I need to select one. However, the dropdown's behavior varies depending on the renderValue. There are 3 different cases: Case 1 renderValue={value => <img src={value.c ...

"Padding has not been recognized as a valid input value

I'm struggling with getting padding to be accepted by the browser when I style a card using Material-UI. const useStyles = makeStyles((theme) => ({ cardGrid: { padding: '20px auto' }, })) Unfortunately, the browser is not recogni ...

Navigating through Next.js pages feels like dragging through molasses, especially when using server

It's clear that this question has been asked before, but the existing answers are not solving my issue. The problem I'm facing is that server-side rendering is taking too long. Moving from page to page is slow, ranging from 2.5-4 seconds and som ...

Can a "fragile export" be generated in TypeScript?

Testing modular code can be challenging when you have to export things just for the purpose of testing, which can clutter your code and diminish the effectiveness of features like "unused variable" flags on compilers or linters. Even if you remove a usage ...

Are the frameworks Vue, Angular, and React known for

During a conversation, I came across an interesting viewpoint criticizing popular frameworks such as Angular, Vue, and React. It was argued that these frameworks have a significant disadvantage: apart from the API part that interacts with the server's ...

Switch tabs with JavaScript

I'm encountering an issue with changing tabs using JavaScript and Ajax. The script I have works when not using Ajax but fails to work properly with it. Here is the script: $(document).ready(function(){ $("#mtabs li").click(function() { ...

Tips for presenting the retrieved data from the database in a user-friendly format

$ //First step involves running the PHP code that executes the SQL query to retrieve data from the database, storing it in get_row1, and sending it as a response to Ajax$ <?php $connect = mysql_connect("localhost", "root", ""); $data = mysq ...

Text input setting for jQuery UI Slider

Currently, I am utilizing jQuery UI sliders to input values in text boxes. However, I would like this functionality to be bidirectional; meaning if a value is entered into the text box, I want the slider to move to the corresponding position. I am unsure ...

Display a complete calendar with the date picker on a webpage

Currently experimenting with React to build a straightforward application. Admiring the appearance of full calendars from these date pickers (once clicked): Material-UI React-Toolbox Wondering if it's feasible to present the full calendar directly ...

Maintain the expanded sub-menu when the mouse leaves the area, but a sub-option has been

Implementing a side menu with parent and child options that dynamically display content in the main div on the right when a child option is selected. The parent options are initially shown upon page load, and the child options appear when the mouse hovers ...

Organize information in a React table following a predetermined sequence, not based on alphabetical order

As a beginner with React, I'm looking to sort my data by the column "Status" in a specific order (B, A, C) and vice versa, not alphabetically. The data structure looks like this: export interface Delivery { id: number; name: string; amount: num ...