My issue its when i have to click in some tag to filter my list of articles the components BlogFolders re-render again and create new components or even add new ones.
I just what to filter articles whitout affect my tags or in this case my component BlogFolders
this component receive the next prop function
const filterByTag = async (id) => {
const res = await api.get(`article/?tags=${id}`);
setArticles(res.data);
}
ALL CODE
const Blog = () => {
const [articles, setArticles] = useState([]);
const [tags, setTags] = useState([]);
// consult to database
const getData = async () => {
try {
// get aticles, tags
const [res1, res2] = await axios.all([api.get('article/'), api.get('tag/')]);
setArticles(res1.data);
setTags(res2.data);
} catch (e) {
console.log(e);
}
}
useEffect(() => {
getData();
}, [])
// filter the article by tag
const filterByTag = async (id) => {
const res = await api.get(`article/?tags=${id}`);
setArticles(res.data);
}
return (
<>
<Header />
<HeaderPage title="BLOG" banner={imgBanner} breadcrumb="HOME / NEWS" />
<section>
<Grid container gap={5} sx={Style.ContainerGrid}>
<BlogFolders tags={tags} filterByTag={filterByTag} />
<Grid container sx={Style.ContainerArticles}>
<Grid container gap={2} sx={Style.ContainerArticlesItems}>
{articles.length > 0 && articles.map(a =>
<Grid item xs={12} maxWidth="md" md={4} sx={Style.ArticleContainer} key={a.id}>
<img src={a.thumbnail} style={Style.ArticleImage} alt="image" />
<Box mt={2} mb={2}>
<Box mb={1} >
{a.categories.map((c, i) =>
<Typography variant="overline" component="span" color="primary" key={i}>{c.category_name} </Typography>
)}
<span style={Style.ArticleDot}>•</span>
<Typography variant="overline" component="span" color="#333">{a.date_published}</Typography>
</Box>
<Link href="#" color="inherit" underline="none">
<Typography variant="h5" component="h2" sx={Style.TitleArticle}>{a.title}</Typography>
</Link>
</Box>
<Box sx={Style.ArticleContent}>
<Typography variant="subtitle1" paragraph={true} color="#333" sx={Style.TxtArticle}><span dangerouslySetInnerHTML={{ __html: a.first_text }} /></Typography>
</Box>
<Box mt={3} display="flex" alignItems="center">
<Avatar alt="avatar" src="https://images.unsplash.com/photo-1438761681033-6461ffad8d80?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=256&q=80" />
<Box ml={2} textAlign="left">
<Typography variant="subtitle1" component="h2" color="primary">Linda Williams</Typography>
<Typography variant="subtitle1" component="h3" color="textSecondary">Founder and CEO</Typography>
</Box>
</Box>
</Grid>
)}
</Grid>
<Box mt={6} textAlign="center">
<Button variant="outlined" color="primary">todos los articulos</Button>
</Box>
</Grid>
</Grid>
</section>
<Footer />
</>
)}