[英]Do I need a global state in my Reactjs app?
I am working in an app to read the most viewed news in the last 7 days using the NY Times API ( https://developer.nytimes.com/docs/most-popular-product/1/overview ) filtered by sections.我正在使用按部分过滤的 NY Times API ( https://developer.nytimes.com/docs/most-popular-product/1/overview ) 阅读过去 7 天内浏览次数最多的新闻。 You will understand better watching my user interface right now:
现在看我的用户界面,你会更好地理解:
So, the problem is I would like when I click, for instance, on New York section, the news to appear below the sections.所以,问题是当我点击时,例如,在纽约部分,新闻出现在部分下方。 For that I have created my
ListOfNews
component, which would have inside it News
component.为此,我创建了我的
ListOfNews
组件,其中包含News
组件。 But, when I click on a section, what happens it what you can see at the next image:但是,当我单击一个部分时,会发生什么,您可以在下一张图片中看到:
The salmon background color is just to know well size of the box.鲑鱼背景颜色只是为了很好地了解盒子的大小。 Ok.
好的。 Let's jump into the code.
让我们跳入代码。 Here the code of my
Section
component:这是我的
Section
组件的代码:
import React, { useState } from 'react';
import { getNewsAttributes } from '../helpers/helpers';
import getNewsFilteredBySection from '../services/getNewsFilteredBySection';
import ListOfNews from './ListOfNews';
export default function Section({ section }) {
const [news, setNews] = useState(null);
function showEvent(e) {
const sectionSearched = e.target.innerText;
getNewsFilteredBySection(sectionSearched)
.then((response) => {
const filteredResults = response;
const newsAttributes = getNewsAttributes(filteredResults);
setNews(newsAttributes);
})
}
return (
<>
<div className="animate__animated animate__fadeIn animate__slower">
<h3 className="section-container mr-4 mb-4 pointer"
onClick={showEvent}
>{section}</h3>
</div>
{news !== null ? <ListOfNews newsAttributes={news} /> : null}
</>
)
};
Here the code of my ListOfNews
component:这是我的
ListOfNews
组件的代码:
import React from 'react';
export default function ListOfNews({ newsAttributes }) {
return (
<div className="listofnews-container mb-4">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose injected humour and the like.</p>
</div>
)
};
And later I would have to pass the newsAttributes
to the News
component, where I would render the title, the image, and the abstract of the news.稍后我必须将
newsAttributes
传递给News
组件,在那里我将呈现新闻的标题、图像和摘要。 So, here are my questions:所以,这是我的问题:
a) How can I achieve my ListOfNews
component to render below all the sections, no matter which section I click? a) 无论我点击哪个部分,如何实现我的
ListOfNews
组件在所有部分下方呈现? I suppose the problem is currently in my logic, since I am returning this component with the component Section
.我想问题目前在我的逻辑中,因为我正在使用组件返回这个组件
Section
。
b) I suppose, the problem is that logic is implemented in the Section
component, so which could be a good way of implementing this? b)我想,问题是逻辑是在
Section
组件中实现的,那么这可能是实现它的好方法吗? Do you think implementing the logic in a outer helper could work?你认为在外部助手中实现逻辑可行吗? Or maybe should I need a global state manager?
或者也许我需要一个全局 state 经理? In this case... which one do you think would fit the best my needs?
在这种情况下......你认为哪一个最适合我的需求? Redux?
Redux? Maybe useContext would be enough?
也许 useContext 就足够了? Thanks a lot:)
非常感谢:)
I think what you are doing is repeating the Section component right, as a result when you render a section, it also renders the news, and then the next section gets rendered.我认为您正在做的是正确地重复 Section 组件,因此当您渲染一个部分时,它也会渲染新闻,然后渲染下一个部分。 So what you should do is first render sections, and then render the selected section's news.
所以你应该做的是首先渲染部分,然后渲染选定部分的新闻。 In order to do that you need to store the selected section in the outer component.
为此,您需要将所选部分存储在外部组件中。 so move all the non rendering code to outer/parent component, and pass in the eventHandler function
showEvent
to the section component.所以将所有非渲染代码移动到外部/父组件,并将 eventHandler function
showEvent
给部分组件。
Now the outer function will fetch and store the news.现在外部 function 将获取并存储新闻。
your outer functions render should look something like this;你的外部函数渲染应该是这样的;
sections.map(section =>
<Section key={section} section={section} selectHandler={showNews} />
)
{news !== null ? <ListOfNews newsAttributes={news} /> : null}
I hope that makes sense.我希望这是有道理的。
Regarding your second question, looks like you are pretty new to react, so I would recommend not to do that yet.关于你的第二个问题,看起来你对反应很陌生,所以我建议不要这样做。 If your project is just this component you definetely dont need it, if you have a lot of component that need to share state, you might need one, context can definetely help, but I see that you are fetching some data, and react-query might also help, as it does some caching, and fairly well documented and not so hard to start with.
如果您的项目只是这个组件,您肯定不需要它,如果您有很多组件需要共享 state,您可能需要一个,上下文可以明确帮助,但我看到您正在获取一些数据,并且 react-query也可能会有所帮助,因为它会进行一些缓存,并且记录得很好,而且开始起来并不难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.