[英]why I have to make empty function in react context object?
I'm trying to understand reacjs context API from docs.我正在尝试从文档中了解 reacjs 上下文 API。 In the provided example on Updating Context from a Nested Component I found they created a context object like that :
在提供的从嵌套组件更新上下文的示例中,我发现他们创建了一个这样的上下文对象:
export const ThemeContext = React.createContext({
theme: themes.dark,
toggleTheme: () => {},
});
So I asked what is the role of toggleTheme: () => {}
.于是我问了
toggleTheme: () => {}
的作用是toggleTheme: () => {}
。 I found two live example for this snippet of code, They both using the same expression of empty function/method .我为这段代码找到了两个实时示例,它们都使用相同的 empty function/method 表达式。 I tried to remove it at all to see where it affect ... and I found nothing.
我试图完全删除它以查看它影响的位置......但我什么也没找到。 Every thing working good, that makes me curious to know what it doing?
一切都很好,这让我很想知道它在做什么?
Thanks a lot...非常感谢...
At a first glance one can understand that in this context you can change a theme.乍一看,您可以理解在这种情况下您可以更改主题。 As it is provided in the context object.
因为它是在上下文对象中提供的。
This is a method you are providing in the context:这是您在上下文中提供的方法:
toggleTheme: () => {},
if you need to change the theme, then you know that context is providing you one method named toggleTheme
and as it is a method you can trigger it on the basis of your particular event such as mouseevents, keyboardevents
or redux actions
if it is implemented there.如果您需要更改主题,那么您知道上下文为您提供了一个名为
toggleTheme
方法,因为它是一种方法,您可以根据您的特定事件(如mouseevents, keyboardevents
事件mouseevents, keyboardevents
事件或redux actions
触发它,如果它在那里实现的话.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.