+ 1

Can anyone solve my reactjs query?

Need little help

26th Jun 2023, 8:04 AM
Akash Gupta
Akash Gupta - avatar
10 odpowiedzi
+ 1
Show your attempt
26th Jun 2023, 8:05 AM
Hasnain [ACTIVE CHALLENGER]
Hasnain [ACTIVE CHALLENGER] - avatar
+ 1
<Grid item xs={12} md={6}> <Controller name="languageId" control={control} rules={{ required: "language Field is required!", }} render={({ field: { value, onChange } }) => ( <FormControl fullWidth required error={errors?.languageId ? true : false} sx={{ label: { color: "rgba(0, 0, 0, 0.5)", fontWeight: "400", }, }} size="small" > <Autocomplete onChange={(event, newValue) => onChange(newValue)} options={language} getOptionLabel={(option) => option.name} renderInput={(params) => (
26th Jun 2023, 8:13 AM
Akash Gupta
Akash Gupta - avatar
+ 1
<TextField {...params} variant="outlined" label="Market*" size="small" /> )} /> {errors?.languageId && ( <FormHelperText> {errors?.languageId ? errors.languageId.message : null} </FormHelperText> )} </FormControl> )} /> Not able to share whole code in one comment so my problem is that in this code. Language is not saving when i do next operation in my project
26th Jun 2023, 8:15 AM
Akash Gupta
Akash Gupta - avatar
26th Jun 2023, 8:33 AM
Akash Gupta
Akash Gupta - avatar
+ 1
Anyone help me
26th Jun 2023, 9:01 AM
Akash Gupta
Akash Gupta - avatar
+ 1
It seems that you are using the React Hook Form library along with Material-UI components in your React.js project. To address the issue where the "language" value is not being saved, you need to make sure that you are properly handling the form submission and updating the form state with the selected language value. Here are a few steps you can follow to troubleshoot the problem: 1. Make sure you have imported the necessary dependencies in your component: javascript import { useForm, Controller } from "react-hook-form"; import { Autocomplete, TextField, FormControl, FormHelperText } from "@mui/material"; 2. Verify that you have set up the form correctly using the useForm hook: javascript const { control, handleSubmit, formState: { errors } } = useForm(); 3. Ensure that you have defined the `language` array or object that provides options for the Autocomplete component. Check if it is properly populated with the necessary data
27th Jun 2023, 7:17 AM
Abhijeet
Abhijeet - avatar
+ 1
In the Autocomplete component, update the onChange handler to invoke the onChange function provided by the Controller component. This will update the form value for the "languageId" field: javascript <Autocomplete onChange={(event, newValue) => onChange(newValue)} options={language} getOptionLabel={(option) => option.name} renderInput={(params) => ( <TextField {...params} variant="outlined" label="Market*" size="small" /> )} /> ----------------------------------------------------- Ensure that you have a submit handler function defined for your form and that it handles the form submission correctly. For example: javascript const onSubmit = (data) => { console.log(data); // Check if the "languageId" field value is present in the data object // Perform further operations or API calls with the form data }; // Render your form with the submit handler return ( <form onSubmit={handleSubmit(onSubmit)}> {/* Rest of your form components */} </form> );
27th Jun 2023, 7:19 AM
Abhijeet
Abhijeet - avatar
0
Abhijeet when i console the value it giving me 1 but when i console the language than it giving an object of language . But we need to return an object I'm stuck there ..... Literally spend too much time but didn't fix that
1st Jul 2023, 1:49 PM
Akash Gupta
Akash Gupta - avatar
0
If I understand correctly, you want to return an object containing the selected language instead of just the language ID. To achieve this, you can modify the `onChange` handler of the `Autocomplete` component to return the entire `option` object instead of just the `newValue`. Here's an updated code snippet: javascript <Autocomplete onChange={(event, newValue) => onChange(newValue)} options={language} getOptionLabel={(option) => option.name} renderInput={(params) => ( <TextField {...params} variant="outlined" label="Market*" size="small" /> )} /> ``` Replace the `onChange` handler with the following code: javascript <Autocomplete onChange={(event, newValue) => { onChange(newValue); // Update the form value with the selected option }} // Rest of the code... />
3rd Jul 2023, 2:11 PM
Abhijeet
Abhijeet - avatar
0
Akash Gupta By doing this, the onChange handler will receive the entire option object, which includes both the language ID and other properties like name. The newValue will now contain the selected language object instead of just the ID. Ensure that you make this change in the onChange handler of the Autocomplete component and re-test your application. You should now be able to retrieve the complete language object with all its properties instead of just the ID.
3rd Jul 2023, 2:13 PM
Abhijeet
Abhijeet - avatar