T
T
TheSerKat2021-03-16 11:05:43
JavaScript
TheSerKat, 2021-03-16 11:05:43

How to fix Warning: A component is changing an uncontrolled input to be controlled?

//Изначально приходит ''
 const contactsSelector = useSelector(state => getProfileState(state))
 let [contact, setContact] = useState('')
    useEffect(() => {
        setContact(contactsSelector)
    }, [contactsSelector])
//Потом приходит объект в нем свойства и объекты и это все присваивается в локальный contact
return(
     <Formik
          enableReinitialize
           initialValues={ {...contact} } //И тут проблема тут должно быть по умолчанию ''

//initialValues={'' || {...contact}} не работает

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2021-03-16
@Casufi

1) Why do you initially assign an empty string, if then you have an object there ? If you know the structure of the contact object, make an empty structure with nulls in the fields. If I'm wrong, and contact ends up with a string, then it's completely incomprehensible why you are trying to destructure it as an object. 2) You initially assign empty. a line in contact and then try to destructure it {...contact} you read books how it works or you copy someone else's code? https://developer.mozilla.org/en-US/docs/Web/JavaS... 3) In the error message, the component that caused the error is usually written, and in your case it is clearly some kind of input element, and you write that Formic level problem. 4) //useState('')
initialValues={'' || {...contact}} не работаетRead how the || operator works and what equals Boolean("") this code will never work.
The problem is that you do not know elementary things in JS Core, but you are trying to use destructuring, side effects in the comparison operators, start from the book, just read the Javascript base

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question