再会!
您需要创建一个接受参数 + 嵌套 html 元素和其他组件的组件
有一个组件:
import { Component } from 'react'
import styled from 'styled-components';
export class ListItem extends Component {
constructor(props) {
super(props)
this.state = {
id: this.props.id,
title: this.props.title
}
}
render() {
const { id, title } = this.state
console.log(title);
return (
<Li key={id}>
<Span>{title}</Span>
//<Component {..this.props}/> вызывает ошибку. без этой строки компонент работает но не отображает вложенные компоненты
</Li>)
}
}
const Li = styled.li`
color: rgba(0, 0, 0, 0.87);
border: none;
cursor: default;
height: 32px;
display: inline-flex;
outline: 0;
padding: 0;
font-size: 0.8125rem;
box-sizing: border-box;
transition: background-color 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
align-items: center;
font-family: "Roboto", "Helvetica", "Arial", sans-serif;
white-space: nowrap;
border-radius: 16px;
vertical-align: middle;
justify-content: center;
text-decoration: none;
margin: 2px;
background-color: #e0e0e0;
`;
const Span = styled.span`
overflow: hidden;
white-space: nowrap;
padding-left: 12px;
padding-right: 12px;
text-overflow: ellipsis;
`;
我像这样连接组件:
import { ListItem } from './UI/li_grey'
...
<ListItem key={i.id} title={i.name}><button className={styles.button_item_admin_menu} onClick={(e) => this.DeleteItemMenu(i.id, e)}>x</button></ListItem>
我将不胜感激您的帮助!
我找到了一个解决方案......要将子元素传递给组件,您需要使用 not
<Component {..this.props}/>a{children}( const { children } = this.props;