I'm trying to update category item in the list. when i send fetch request to the server its returning response, But when i use that data on text input, It's showing data is undefended. How to fix this issue?
[![
const { register, handleSubmit, watch, formState: { errors } } = useForm();
const [data, setData] = useState([]);
const category_id = useParams();
useEffect(() => {
axios.get(`api/admin/category/${category_id.id}`)
.then(res => {
setData(res.data)
})
},[])
console.log(data)
function updateCategory() {
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
try consol logging data.something like data.id or smthn and when you input the data maybe don't do data.data.attributes.name i think you can just do data.attributes.name since you already saved the first data in the state, if this doesn't work show us were you input the data
set data as a dependency in you useEffect to to make useEffect watch for changes in the state
const [data, setData] = useState([])
useEffect(() => {
async function GetData() {
var response = await axios.get(`api/admin/category/${category_id.id}`) #changed the link to what you used
setData(response.data)
}
GetData()
}, [])
console.log('data', data)
}