鍍金池/ 問答/數(shù)據(jù)分析&挖掘  網(wǎng)絡(luò)安全  HTML/ material-ui里面的withStyles是什么?

material-ui里面的withStyles是什么?

https://github.com/mui-org/ma...

export default withStyles(styles, { name: 'MuiAppBar' })(AppBar);
//這里的作用是什么?
回答
編輯回答
萢萢糖

withStyles 是一個(gè) HOC 組件,會(huì)為你的 AppBar 組件根據(jù)當(dāng)前的 theme 來添加樣式。核心功能就是為子組件提供了一個(gè) classes props,這樣你就可以在外部對(duì) class name 進(jìn)行修改。

在你這個(gè)例子中,就會(huì)將第一個(gè)參數(shù) styles 的樣式,覆蓋掉原來主題中的 MuiAppBar 樣式。

2018年6月5日 00:17
編輯回答
安淺陌

樓上的回答是正確的,不過利于理解,先說下 Material-UI 中默認(rèn)支持的樣式吧,使用的是 CSS-In-JS 方案,也就是 JSS, 而你寫的樣式都是 Object, 所以,需要把你的對(duì)象 JSS to classes,就是 JSS 利用你的 object 生成樣式,并且把所有的 classnames 成為一個(gè)對(duì)象為 classes 通過 props 傳遞給你的下一級(jí)組件。

const styles = { root: { width: '100%' } };

-> CSS :

ComponentName-root_0 { width: 100%; };

-> classes

const classes = { root: 'ComponentName-root_0' };

withStyles(stypes) 步驟完成你的完整代碼是:withStyles(stypes)(Component) 如下(withStyles(stypes) 代碼如下):

return (Component) => (props) => (<Component {...props} classes={classes} />);

現(xiàn)在明白了吧?

2018年9月2日 13:46