2017-04-24 14 views
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) 

答えて

1

にブラウザで取得エラーが親で

アプローチ1

、これを試してみてくださいされています

render() { 
    return <SortableItem {...extraProps} item={<YourComponent />} /> 
} 

インSortableItemレンダリング:

render() { 
    return {this.props.item} 
} 

アプローチ2:親で

render() { 
    return <SortableItem {...extraProps} > 
    <YourComponent /> 
    </SortableItem> 
} 

をSortableItemでレンダリング:

render() { 
    return {this.props.children} 
} 
+0

ありがとうございました。 React.cloneElement(item、extraProps)を ''にするにはどうすればいいですか? – skrln

+0

正確に複製すると、コードを共有できますか? @skrln –

+0

**上位コンポーネント** '

    {items.map(item、index)=> { const extraProps = { disabled:、sortable 、 key: 'item-' + index、 インデックス:インデックス、 }; return } )}
マップの 'items'は、この親コンポーネントの元の子です。 私はこの親からの小道具(ソート可能)も、子どもたちにもいくつかの余分な小道具(キーと索引)と共に必要とするので、これを行う。 'のconst SortableItem =(小道具)=> { 戻り小道具: – skrln

関連する問題