0
私の実装では、反応要素(少なくとも私が思うもの)を小道具として渡します。オブジェクトをオブジェクトとして返す
この小道具を自分のリアクションコンポーネントとしてレンダリングしたいと思いますが、ブラウザがオブジェクトであると不平を言います。
コードは、明確にする:
:
はrender() {
return <SortableItem {...extraProps} item={React.cloneElement(item, extraProps)} />
}
item
小道具は私がレンダリングSortableItemでSortableItem
でレンダリングする要素が含まれています。親に
私はいくつかしたいこのようなもの:
render() {
return <props.item />
}
私はprops.item
をログに記録するとき、私はこれを取得:
Object {$$typeof: Symbol(react.element), key: "item-5", ref: null, props:
Object, type: function…}
$$typeof:Symbol(react.element)
key:"item-5"
props:Object
ref:null
type:function _class()
私は$$typeof
が、これは確かに反応する要素であると言うだろう理由として混乱していますが、種類は、それがfunction _class()
とするときだと言います私はログを記録する/それはオブジェクトであることをブラウザに表示します。
この<props.item />
をレンダリングするとき、私はSortableItem
Fatal Exception:Uncaught Invariant Violation: Element type is invalid:
expected a string (for built-in components) or a class/function (for
composite components) but got: object. Check the render method of
`SortableItem`.(reload cancelled)
ありがとうございました。 React.cloneElement(item、extraProps)を ' 'にするにはどうすればいいですか? –
skrln
正確に複製すると、コードを共有できますか? @skrln –
**上位コンポーネント** '
{items.map(item、index)=> { const extraProps = { disabled:、sortable 、 key: 'item-' + index、 インデックス:インデックス、 }; return } )}
マップの 'items'は、この親コンポーネントの元の子です。 私はこの親からの小道具(ソート可能)も、子どもたちにもいくつかの余分な小道具(キーと索引)と共に必要とするので、これを行う。 'のconst SortableItem =(小道具)=> { 戻り小道具: – skrln