Reactjs 如何使用massy.js和React.js?
我试图在react.js中集成massy.js,但我遇到了设计问题 我应该把所有的砖块都放在一个根反应组件中吗Reactjs 如何使用massy.js和React.js?,reactjs,jquery-masonry,react-masonry,Reactjs,Jquery Masonry,React Masonry,我试图在react.js中集成massy.js,但我遇到了设计问题 我应该把所有的砖块都放在一个根反应组件中吗 this.props.elements.map(函数(元素){ 返回( ); }); 或者我应该将每个砖块作为一个新的React组件作为根React组件的子组件 <RootComponent> this.props.elements.map(function(element){ return ( <
this.props.elements.map(函数(元素){
返回(
);
});
或者我应该将每个砖块作为一个新的React组件作为根React组件的子组件
<RootComponent>
this.props.elements.map(function(element){
return (
<ChildComponent props={element}>
);
});
<RootComponent>
this.props.elements.map(函数(元素){
返回(
);
});
react Massiness component如果您希望始终使用Massiness,则该组件很好,但如果您希望根据某些条件初始化Massiness,则可以执行以下操作:
例如,您获取一些项目,然后初始化:
componentWillMount() {
this.props.fetchItems();
}
componentDidUpdate(prevProps) {
const {
items
} = this.props;
if (this.state.masonry && prevProps.items !== items) { // reinitialize if items change
$('.list').masonry('destroy');
this.setState({
masonry: false
});
}
if (someCondition && items.length && !this.state.masonry) {
$('.list').masonry({
itemSelector: '.list__item'
});
this.setState({
masonry: true
});
}
}
您的渲染可以如下所示
render() {
const {
items
} = this.props;
return (
<div className="list">
{items.map(item =>
<div className="list__item" key={item.id} >
{item.title}
</div>
)}
</div>
);
render(){
常数{
项目
}=这是道具;
返回(
{items.map(item=>
{item.title}
)}
);
}为什么不使用组件
render() {
const {
items
} = this.props;
return (
<div className="list">
{items.map(item =>
<div className="list__item" key={item.id} >
{item.title}
</div>
)}
</div>
);