5C6 purushotham Reddy
5C6 purushotham Reddy

Reputation: 43

anyone help me with this in react , old values are again console logging with the present value selected value

when i tried to store status value in the values state and then try to access it but it is showing previous values with the present value

and i got an error like this

Error: Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components at input at div at App (https://aid-

`


import React, { useState , useEffect } from "react";
import "./App.css";

function App() {

    //values

    const [values, setValues] = useState({
        title: "",
        owner: "",
        status: "",
        effort: "",
        due: ""

    })

    //seting values

    const settingValues=(e)=>{
        setValues({[e.target.name]:e.target.value})
    }

    useEffect(()=>{
        console.log(values.status)
    },values)

    const style = { display:"flex",flexDirection:"column",padding: "5px",margin:"2px" }

    return (
        <div className="App" style={style}>
            <input type="text" name="title" value={values.title} onChange={(e)=>settingValues(e)} placeholder="title" style={style} />
            <input type="text" name="owner" value={values.owner} onChange={(e)=>settingValues(e)} placeholder="owner" style={style} />
            <select  name="status" value={values.status} onChange={(e)=>settingValues(e)}  style={style}>
                <option value="Status">status</option>
                <option value="New">New</option>
                <option value="Assaigned">Assaigned</option>
                <option value="Fixed">Fixed</option>
                <option value="Closed">Closed</option>
            </select>
            <input type="number" name="effort" value={values.effort} onChange={(e)=>settingValues(e)} placeholder="effort" style={style} />
            <input type="date" name="due" value={values.due} onChange={(e)=>settingValues(e)} style={style} />
        </div>
    );
}

export default App;

`

Upvotes: 1

Views: 70

Answers (1)

jhn_bdrx
jhn_bdrx

Reputation: 36

UseEffect is expecting a dependancy array as a second parameter, but it looks like you're giving it an object from state.

useEffect(()=>{
        console.log(values.status)
    },[values])

Also, checkout this post. I found it helpful when using useEffect

Upvotes: 1

Related Questions