Home  >  Q&A  >  body text

Use ReactJS TypeScript MUI, use useState to set the value of TextField and add onchange event function for TextField

<p>I am a newbie developing with ReactJS and MUI and have a ReactJS TypeScript with MuiText field form. Want to be able to use the <code>useState</code> method to change the value of a text field. </p> <p>Also add the <code>onchnage</code> function to the text field.I can add onchange function for normal textfield but not sure how to add it for MUI textfield?</p> <pre class="brush:php;toolbar:false;">import * as React from 'react'; import { useState } from "react" import Button from '@mui/material/Button'; import CssBaseline from '@mui/material/CssBaseline'; import TextField from '@mui/material/TextField'; import Grid from '@mui/material/Grid'; import Box from '@mui/material/Box'; import Container from '@mui/material/Container'; import { createTheme, ThemeProvider } from '@mui/material/styles'; import { useForm, SubmitHandler, Controller } from 'react-hook-form'; import * as yup from 'yup'; import { yupResolver } from '@hookform/resolvers/yup'; interface IFormInputs { filepath: string; } const schema = yup.object().shape({ filepath: yup.string().min(4).required(), }); const theme = createTheme(); export default function MuiTextField() { const { control, handleSubmit, formState: { errors }, } = useForm<IFormInputs>({ resolver: yupResolver(schema), }); const [filepath, setFilepath] = useState("vodeo.mp3"); const onSubmit: SubmitHandler<IFormInputs> = (data) => { console.log('data submitted: ', data); console.log('filepath: ', data.filepath); }; return ( <ThemeProvider theme={theme}> <Container component="main" maxWidth="lg"> <CssBaseline /> <Box sx={{ marginTop: 8, display: 'flex', flexDirection: 'column', alignItems: 'center', }} > <form onSubmit={handleSubmit(onSubmit)}> <Box sx={{ mt: 3 }}> <Grid container spacing={2}> <Grid item xs={16} sm={6}> <Controller name="filepath" control={control} defaultValue="" render={({ field }) => ( <TextField {...field} label="File Path" error={!!errors.filepath} helperText={errors.filepath ? errors.filepath?.message : ''}autoComplete="file-path" fullWidth /> )} /> </Grid> <Button type="submit" variant="contained" sx={{ mt: 3, mb: 2 }} > Submit </Button> </Grid> </Box> </form> </Box> </Container> </ThemeProvider> ); }</pre> <p>Update: This is the codeshare link: https://codesandbox.io/s/boring-water-m47uxn?file=/src/App.tsx</p> <p>When we change the value of the text box to <code>auto</code>, we want to change the value of the text box to <code>audio.mp3</code>, but it doesn't work. </p>
P粉212114661P粉212114661389 days ago421

reply all(1)I'll reply

  • P粉969666670

    P粉9696666702023-08-29 19:47:05

    MUI Textfield also has onChange:

    <TextField
         error={Boolean(touched.name && errors.name)}
         fullWidth
         label={t('Name')}
         name="name"
         onBlur={handleBlur}
         onChange={handleChange}
         value={values.name}
         variant="outlined"
         autoComplete="off"
    />

    The 'field' in the render function contains onChange. The state of the form is saved in useForm. In useForm's props you need to add defaultValues. You are not passing prop type="file", may be your problem.

    Guide to creating file input using react hook form: https://refine.dev/blog/how-to-multipart-file-upload-with-react-hook-form/

    Textfield API: https://mui.com/material-ui/api/text-field/

    reply
    0
  • Cancelreply