I setup a project with laravel as backend Api provider and React as Frontend manager. React stateless functional component work well when I simply render some component.
const [state, setState] = useState({data:someData})
I use useEffect to fetch some data with Axios from an API URL. fine
But when I change the state of a component with setState, it does not work. No change is reflected. it looks like a static page even if I hit 100 times the button which has the click event. the console is clear. No errors
I want to know why? and how can I fix it.
Note: Class based component does not have this problem. it perfectly renders any update of the state
Some codes
const Users = (props) => {
const [state, setState] = useState({
dataPraticiens: {
data: [],
currentPage: 1,
PerPage: 3
},
loaded: false
});
useEffect(() => {
Axios.get('/api/users').then((r) => {
setState({
dataPraticiens: {
data: [...r.data.docs],
currentPage: 1,
PerPage: 3
} ,
loaded: true
});
}).catch((er)=> {
console.log(er);
})
},[state.loaded]);
const changePage = (event) => {
let nS = state;
nS.dataPraticiens.currentPage = Number(event.target.id);
nS.loaded = false;
setState(nS);
// log the state and yeah, state changes but not reflected in the page
}
const indexOfLast = state.dataPraticiens.currentPage * state.dataPraticiens.PerPage;
const indexOfFirst = indexOfLast - state.dataPraticiens.PerPage;
const current = state.dataPraticiens.data.slice(indexOfFirst, indexOfLast);
const pageN = [];
for (let i = 1; i <= Math.ceil(state.dataPraticiens.data.length / state.dataPraticiens.PerPage); i++) {
pageN.push(i);
}
const renderPageN = pageN.map(num => {
return (
<li
key={num}
id={num}
onClick={changePage} // Not work here.
>
{num}
</li>
);
});
return (
{current.map((value, index) => {
return <React.Fragment key={index}>
<li>{value}</li>
</React.Fragment>
})}
<ul>
{renderPageN} // pages number`
</ul>
)}