const [searchParams, setSearchParams] = useSearchParams();
useEffect(() => {
setSearchParams(...);
}, []);
const cancelToken = axios.CancelToken;
const usePost = () => {
const source = useRef(null);
return () => {
if (source.current) {
source.current.cancel();
}
source.current = cancelToken.source();
const result = http.post(`${API_URL}.${METHODS.CALL_ENROLLMENT}`, { cancelToken, userId, filter });
// ........
result.finally(() => {
// Обнуляем токен при завершении запроса
source.current = null;
});
return result;
}
}
const first_name = document.getElementById("input_f_n").value;
const last_name = document.getElementById("input_l_n").value;
const email = document.getElementById("input_e").value;
const houseID = document.getElementById("input_houseID").value;
const userID = (users.filter(word => word.userID === index))[0].userID;
const fields = { userID, first_name, last_name, houseID, email };
const body = Object.keys(fields).reduce((acc, key) => {
if (fields[key] !== '' && fields[key] !== null && fields[key] !== undefined) {
acc[key] = fields[key];
}
return acc;
}, {});
const response = await fetch('http://127.0.0.1:8000/api/users/user/update', {
method: 'PUT',
headers: {'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest',},
credentials: 'include',
body: JSON.stringify(body),
});
//...
const body = { userID, first_name, last_name, houseID, email };
Object.keys(body).forEach(key => {
if (body[key] === '' || key[key] === null) {
delete body[key];
}
});
//...
//...
const body = {};
if (first_name !== '') {
body.first_name = first_name;
}
// Дальше проверки по всем значениям
// ...
import React, { useEffect, useState } from 'react';
// Функция-инициализатор нужна, чтобы вынести создание объекта состояния из компонента
const initStyle = () => ({ zIndex: 0 });
const Component = () => {
const [style, setStyle] = useState(initStyle);
useEffect(() => {
// Вот здесь можно менять значения - zIndex поменяется через секунду
const timer = setTimeout(() => setStyle({ zIndex: 1 }), 1000);
// Надо не забыть почистить счетчик setTimeout
return () => clearTimeout(timer);
}, []);
return <header className={`${styles.header} navbar navbar-expand-lg navbar-light sticky-top`} style={style} />
};
import React, { useEffect, useMemo, useState } from 'react';
const Component = () => {
const [zIndex, setZIndex] = useState(0);
// Поскольку style это объект, то неплохо бы его мемоизировать
const style = useMemo(() => ({ zIndex }), [zIndex]);
useEffect(() => {
const timer = setTimeout(() => setZIndex(1), 1000);
return () => clearTimeout(timer);
}, []);
return <header className={`${styles.header} navbar navbar-expand-lg navbar-light sticky-top`} style={style} />
};
class WS extends React.Component {
constructor(props) {
super(props);
this.state = { messages: [] };
this.ws = new WebSocket("ws://api...");
}
componentDidMount() {
this.ws.onmessage = responce => this.setState({ ...this.state, messages: [ ...this.state.messages, responce.data ] });
}
componentWillUnmount() {
this.ws.close();
}
}
import React, { useEffect, useState } from 'react';
function ws() {
const [message, setMessages] = useState([]);
useEffect(() => {
const ws = new WebSocket("ws://api...");
ws.onmessage = responce => this.setState({ ...this.state, messages: [ ...this.state.messages, responce.data ] });
return () => ws.close();
}, []);
}
/* eslint-disable jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions */