Invariant Violation: React.Children.only expected to receive a single React element child

半城伤御伤魂 提交于 2019-12-06 18:08:18

问题


I keep getting the invariant violation and I really don't know why ... it definitely has to do with the OverlayTriggers. When leaving them out everything works fine.

Imports:

import Col from 'react-bootstrap/lib/Col';
import Row from 'react-bootstrap/lib/Row';
import Tooltip from 'react-bootstrap/lib/Tooltip';
import OverlayTrigger from 'react-bootstrap/lib/OverlayTrigger';

supposed to render a list item with a Key/Value pair, where the Value has a tooltip:

const renderCustomField = (field,customFields) => {
    const tooltip = (<Tooltip id="tooltip">{customFields[field]['sub']}</Tooltip>)
    return (
        <li>
            <OverlayTrigger placement="top" overlay={tooltip}>
                {field}
            </OverlayTrigger>
        </li>
    )
}

The class that wants to render the customFields:

export default class EventHeaderCustomFields extends Component {
    render () {
        const customFieldsNames = Object.keys(this.props.customFields);

        return (
            <Col xs={12}>
                <h4><strong>Short overview:</strong></h4>
                <ul>
                {customFieldsNames.map(
                    (field) => renderCustomField(field,this.props.customFields)
                )}
                </ul>
            </Col>
        )
    }
}

EventHeaderCustomFields.propTypes = {
    eventData:PropTypes.object
};

回答1:


OverlayTrigger expects a React element child, wrapping {field} in span or any other valid React jsx element will fix this.

<li>
    <OverlayTrigger placement="top" overlay={tooltip}>
        <span>{field}</span>
    </OverlayTrigger>
</li>


来源:https://stackoverflow.com/questions/45112294/invariant-violation-react-children-only-expected-to-receive-a-single-react-elem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!