[英]Why i get [Getter/Setter] on first object in console.log?
i'm tried to create theme for my app, and this is a sample of my theme:我试图为我的应用程序创建主题,这是我的主题示例:
//themeContext.js
export const buildTheme = (userColors = {}, userFonts = {}, userTheme = {}) => {
const mergedColors = {
...defaultColors,
...userColors,
};
// merge fonts
const mergedFonts = {
...defaultFonts,
...userFonts,
};
return {
colors: mergedColors,
fonts: mergedFonts,
label: {
marginTop: 10,
fontSize: 14,
color: mergedColors.textPrimary,
...(userTheme.label)
},
// error
error: {
fontSize: 12,
color: mergedColors.error,
...(userTheme.error),
},
//another style
}
}
export const ThemeContext = createContext({
theme:buildTheme()
});
and on DynamicForm.js i use the themeContext
like this:在 DynamicForm.js 上,我像这样使用themeContext
:
export default class DynamicForm extends Component{
constructor(props) {
super(props);
this.state = {
theme:this.props.theme?this.props.theme:buildTheme(),
responses: {},
};
}
render(){
return(
<ThemeContext.Provider value={this.state}>
<TextField
//handle prop
/>
</ThemeContext.Provider>
)
}
}
i have a consumer file, and about to use the theme with this code:我有一个消费者文件,并且即将使用此代码的主题:
export default class TextField extends PureComponent {
static contextType = ThemeContext
render(){
const {theme} = this.context
console.log(theme)
return(
//render component
)
}
}
and the result of console.log(theme)
:和console.log(theme)
的结果:
as you see on label
i get [Getter/Setter]
but not in every value of key, why this is happened?正如你在label
看到的,我得到[Getter/Setter]
但不是在每个键值中,为什么会发生这种情况?
This is because marginTop
, fontSize
, and color
are not just simple properties but they are JavaScript Accessors , which have getters and setters - basically, they are functions to get and set the value.这是因为marginTop
、 fontSize
和color
不仅仅是简单的属性,而且它们是JavaScript Accessors ,它们具有 getter 和 setter - 基本上,它们是获取和设置值的函数。
If you want to force it to call the getters to get the values, you could do this instead:如果你想强制它调用 getter 来获取值,你可以这样做:
console.log(Object.assign({}, theme));
A possible alternative is:一个可能的选择是:
console.log(JSON.stringify(theme));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.