如果我想使用 Context 来存储从更复杂的 API 响应中解析出来的数据,我应该在哪里解析 API 响应?
React 文档中的 Theme example 相当简单:
export const themes = {
light: {
foreground: '#000000',
background: '#eeeeee',
},
dark: {
foreground: '#ffffff',
background: '#222222',
},
};
export const ThemeContext = React.createContext(
themes.dark // default value
);
如果这个 themes
对象来自 API 并且我需要先对它进行一些额外的解析,那么最好的地方在哪里?
value
已经被解析了吗?<ThemeContext.Provider value={myParsedData}>
<Toolbar changeTheme={this.toggleTheme} />
</ThemeContext.Provider>
答案是否会根据 theme
值是否改变而改变?