-1
I am facing a problem with upgrading a State using React Hooks. The problem is that subsequently
the application of a .filter()
I try to apply the resulting value of the filtering performed in a setProducts() and when I do a console.log() to check the products of the setProducts it is empty.
NOTE: When I put the Debugger to visualize what is happening, I realize that the setProducts receives the value normally...image example 1/image example 2
const [original, setOriginal] = useState([]); //recebe os valores da API sem nenhum filtro
const [products, setProducts] = useState([]); //aqui deve receber os valores após o filtro
const [apply, setApply] = useState(false);
useEffect(() => {
async function loadProducts() {
const response = await api.get("/product");
setOriginal(response.data.product);
}
loadProducts();
}, []);
const handleChange = event => {
const { value, name } = event.target;
var filtered = original.filter(
el => el[name] === value || el[name] === parseInt(value)
);
setApply(true);
setProducts(filtered);
console.log("orginal", original);
console.log("filtrado", filtered);
console.log("products", products);
debugger;
};
I am a layman in React, I may be writing more nonsense, but put the setProducts() code because the original array has items, filtered an item so the filter worked leading me to believe that setProducts() was the one who failed.
– Augusto Vasques