2017-10-20 17 views
0

これは何をしようとしているのかの例です。 MyComponentを他のライブラリに渡しているので、<MyComponent hello={'Hello World'}/>を使わずにMyComponentクラスの小道具を渡したいと思います。reactjs、クラスレベルで小道具を渡す方法

react-reduxはthis.props.stateを追加するためにどのように接続し、他の機能をコンポーネントの小道具にマッピングしますか?カスタムオブジェクトをコンポーネントに挿入したい

+0

あなたが求めていることを考えてください。あなたは小道具を渡すことなく、小道具を渡したい。いいえ、それはあなたがリアクションの方法論をどのように使うのかではありません。あなたの例では 'let props = {...}'と言って、それがうまくいくので ''を使ってください:これをしたくない理由はありません。プログラミングの観点から見てください。 –

答えて

1

あなたはHigher Order Component

function withMyObject(WrappedComponent,helloText) { 
    return class extends React.Component { 

    render() { 
     return <WrappedComponent {...this.props} hello={helloText} />; 
    } 
    }; 
} 

使用法を作成する必要があります:あなたは、デフォルトの小道具を設定することができ

UpdatedComponent = withMyObject(ComponentToBeWrapped,"Hello"); 
2

を。私はその良い習慣だと思う。

import React, { Component } from 'react'; 

class MyComponent extends Component { 
    render() { 
    const { hello } = this.props; 
    return (
     <div>{hello}</div> 
    ); 
    }headerProp: "Header from props...", 
    contentProp:"Content from props..." 
} 

MyComponent.defaultProps = { 
    hello: 'Hello World!' 
} 

<MyComponent /> 
関連する問題