-1

I have this page:

import { Edit, SimpleForm, TextInput, BooleanInput, CheckboxGroupInput, useDataProvider, useNotify } from "react-admin"; import { useEffect, useState } from "react"; import { useParams } from 'react-router-dom'; const UserEdit = () => { const dataProvider = useDataProvider(); const notify = useNotify(); const [roles, setRoles] = useState([]); // Nouvel état pour les rôles sélectionnés const [selectedRoles, setSelectedRoles] = useState([]); const { id } = useParams(); useEffect(() => { dataProvider .getList("roles", { pagination: { page: 1, perPage: 100 }, sort: { field: "label", order: "ASC" }, }) .then(({ data }) => { return setRoles( // @ts-ignore data.map((role) => ({ id: role.id, name: role.label })), ); }) .catch((error) => { console.log(error); notify("Erreur lors du chargement des rôles", { type: "error" }); }); dataProvider.getOne("users", { id: id }) .then(({ data }) => { return setSelectedRoles( // @ts-ignore data.myRoles.map((role) => ({ id: role.id, name: role.label })), ); }) .catch((error) => { console.log(error); notify( "Erreur lors du chargement de l'utilisateur", { type: "error" } ); }); }, [dataProvider, notify]); console.log("roles : ", roles); console.log("selectedRoles : ", selectedRoles); return ( <Edit> <SimpleForm> <TextInput source="email" label="Matricule" /> <TextInput source="firstName" label="Nom" /> <TextInput source="lastName" label="Prénom" /> <CheckboxGroupInput source="myRoles" label="Rôles" choices={roles} defaultValue={selectedRoles} /> <BooleanInput source="published" label="Publié" /> </SimpleForm> </Edit> ); }; export default UserEdit; 

I get this information on the console:

roles : (3) [{…}, {…}, {…}]0: {id: 1, name: 'Utilisateur'}1: {id: 2, name: 'Administrateur'}2: {id: 3, name: 'Super Administrateur'}length: 3[[Prototype]]: Array(0)

user-edit.tsx:48 selectedRoles : (2) [1, 3]0: 11: 3length: 2[[Prototype]]: Array(0)

but the old roles are not checked.

New contributor
abdelouahab dada is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Default values for inputs are always the initial values you want to use from the time the component mounts and renders initially. You are later fetching data and expecting the default values to be different, and this is not how it works. Either fetch your options and values then render the input so it has the initial default values you want, or convert to using a fully controlled input, e.g. use value or whatever prop react-admin uses instead of default values. Please edit to include a complete minimal reproducible example enough for readers to copy/paste and reproduce any issues.Commented5 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.