0
As you can see I created a state called cur and within the map of the mat array, I’m making a request and I want to add the answer inside the event. But when I give console.log down there the state appears empty (setCur does not work). Someone knows how to solve this ?
ps: I created a global array to test and the array takes the values, but when I try to pass the array to the state, the state remains empty.
export function CheckboxesDisciplinas({pessoa, options, ...props}: CheckboxesDisciplinasProps) {
//var nomesDosCursos: Array<string> = [];
const [disciplinas, setDisciplinas] = useState<any[]>([]);
const [cur, setCur] = useState<any[]>([],);
const [disciplinasSelecionadas, setDisciplinasSelecionadas] = useState<any[]>([],);
const classes = styles();
useEffect(() => {
if (pessoa) {
ApiPessoa.findDisciplinasResponsavel(pessoa, options).then(({ data }) => {
const mat = [... new Set(data.map(disc => disc.coddisciplina.toString().replace('/', '-')))]
const unicas = [...new Set(data.map(disc => disc.coddisciplina))].map(
coddisciplina => data.find(disc => disc.coddisciplina === coddisciplina),
);
setDisciplinas(unicas);
mat.map(r => {
Axios.get(`/pessoal/pessoa/${r}/buscarCursos`).then(response => {
//nomesDosCursos.push(response.data.toString())
setCur([...cur, response.data.toString()])
})
})
if (props.initialValues) {
const selecionadas = unicas.filter(disc =>
props.initialValues!.some(coddisciplina => coddisciplina === disc.coddisciplina,),
);
setDisciplinasSelecionadas(selecionadas);
props.onChange(selecionadas);
}
});
} else {
setDisciplinas([]);
setDisciplinasSelecionadas([]);
props.onChange([]);
}
}, [pessoa]);
console.log(cur)
const isDisciplinaSelecionada = (disciplina): boolean => {
return disciplinasSelecionadas.some(
disc => disc.coddisciplina === disciplina.coddisciplina,
);
};
See if this helps you! https://answall.com/questions/460075/ajuda-com-api-useeffect-react
– Colasanto
Edit the question by removing the image and pasting the typed code, then select the code and press Ctrl+K to format it.
– Evilmaax
ready @Evilmaax
– Abner Matheus