在 react 的 render 方法中,我试图根据 state 变量渲染一些孩子。下面引用的是相同的代码:
render: ->
if @state.edit
@adminForm()
else
@adminRow()
if @state.showModal
React.createElement AdminDeleteModal, email: @props.admin.email, handleOK: @handleDelete, handleCancelDelete: @hidePopUpModal
这导致我出现错误:"Uncaught Invariant Violation: ReactCompositeComponent.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object."
但是,当我将这个渲染部分AdminDeleteModal
移到 @adminRow() 方法的末尾时,它可以工作:
adminRow: ->
dom.tr
className: 'row'
dom.td
className: "col-md-6"
@props.admin.email
dom.td
className: "col-md-6 text-right"
dom.a
className: 'glyphicon glyphicon-edit text-primary'
onClick: @handleToggle
dom.a
className: 'glyphicon glyphicon-trash text-danger deletethis'
data: { title: 'Delete Admin?' ,confirm: "Are you sure you want to delete User #{@props.admin.email}?"}
onClick: @showPopUpModal
if @state.showModal
React.createElement AdminDeleteModal, email: @props.admin.email, handleOK: @handleDelete, handleCancelDelete: @hidePopUpModal
虽然,这会导致在 tr 中包含 div 的另一个问题,从而导致以下警告:
Warning: validateDOMNesting(...): <div> cannot appear as a child of <tr>. See (unknown) > tr > (unknown) > div.
更新:使用以下建议更新了代码
render: ->
if @state.edit
@adminForm()
else
@adminRow()
if @state.showModal
dom.tr null,
dom.td null,
React.createElement AdminDeleteModal, email: @props.admin.email, handleOK: @handleDelete, handleCancelDelete: @hidePopUpModal