![](/img/trans.png)
[英]How to make a 'Select' component as required in Material UI (React JS)
[英]How to write a wrapper around a material UI component using React JS?
我正在使用Material UI下一个库,当前正在使用列表组件。 由于该库处于beta版本,因此其许多参数名称都已更改。 为了解决这个问题,我打算围绕必需的组件编写一个包装器,以使事情不会中断。 我的清单组件:
<List dense>
<List className={classes.myListStyles}>
<ListItem disableGutters/>
</List>
</List>
我应该如何为List(例如myListWrapper )和ListItem编写包装器,以便包装器组件可以处理道具并将它们传递给内部的实际MUI列表组件?
我曾经从事过MUI包装程序的工作,为项目编写了自己的库。 我们关注的实现是将props从包装器组件传递到内部/实际MUI组件。 与操纵。 在包装抽象道具时。
以下是我解决方案的方法:
import { List as MaterialList } from 'material-ui/List';
import { React } from 'react';
import { ListItem as MaterialListI } from 'material-ui/ListItem';
class List extends MaterialList {
constructor(props){
const propsToPass = {
prop1 : change(props.prop1),
...props
}
super(propsToPass);
}
};
class ListItem extends MaterialListItem {
const propsToPass = {
prop1 : change(props.prop1),
prop2 : change(props.prop2),
...props
}
super(propsToPass);
}
};
class App extends React.Component {
render () {
return (
<List prop='value' >
<ListItem prop1={somevalue1} prop2={somevalue2} />
<ListItem prop1={somevalue1} prop2={somevalue2} />
<ListItem prop1={somevalue1} prop2={somevalue2} />
</List>
)
}
};
上面的代码将允许您对组件进行以下操作:
您可以要求任何困惑/查询。
您可以这样写:
const MyList = props => (
<List
{/*mention props values here*/}
propA={props.A}
propB={props.B}
>
{props.children}
</List>
)
const MyListItem = props => (
<ListItem
{/*mention props values here*/}
propA={props.A}
propB={props.B}
>
{props.children}
</ListItem>
)
现在,你需要使用MyList
和MyListItem
,决定道具的名称,这些成分(按你的方便),而这些组件映射中的值与实际材料的UI组件的属性。
注意:
如果您为组件使用相同的道具名称(名称与material-ui组件相同),则可以这样编写:
const MyList = ({children, ...rest}) => <div {...rest}>{children}</div>
const MyListItem = ({children, ...rest}) => <p {...rest}>{children}</p>
检查以下示例:
const A = props => <div>{props.children}</div> const B = props => <p>{props.children}</p> ReactDOM.render( <A> <A> <B>Hello</B> </A> </A>, document.getElementById('app') )
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id='app' />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.