1 | initial version |
To assign a value to a text field in Material UI, you can use the "value" prop. Here is an example:
import React, { useState } from "react";
import { TextField } from "@material-ui/core";
function MyTextField() {
const [textValue, setTextValue] = useState("");
const handleTextChange = (event) => {
setTextValue(event.target.value);
};
return (
<TextField
label="My Text Field"
value={textValue}
onChange={handleTextChange}
/>
);
}
In this example, we define a state variable called "textValue" using the useState hook. We initialize it to an empty string. We define a function called "handleTextChange" that updates the "textValue" state whenever the user types something in the text field. Finally, we pass the "textValue" state as the value prop to the TextField component. Whenever the "textValue" state changes, the text field will be updated with the new value.