const input = document.getElementById('input')
input.onkeyup = (e) => {
request(e.target.value);
}
function request(object) {
let response = await fetch('ТУТ ССЫЛКА', {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(object)
});
}
<input type="text" id='input'>
export function useHttp() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null)
const request = useCallback(async (url, method = 'GET', body = null, headers = {}) => {
setLoading(true)
try {
if (body) {
body = JSON.stringify(body)
headers['Content-Type'] = 'application/json'
}
const responce = await fetch(`http://localhost:5000${url}`, {
method, body, headers
})
const data = await responce.json()
setLoading(false)
return data
}
catch(e) {
setLoading(false);
setError(e.messege);
throw e
}
}, [])
const clearError = () => setError(null)
return {error, loading, request, clearError}
}
const change = async (values) => {
const data = await request('/api/settings/change', 'POST', {id, ...values})
console.log(data)
}
<Field type='submit' value='Сохранить' onClick={() => change(values)}/>