プレゼンテーションコンポーネントにコンポーネントのIDに基づいて動的にコンポーネントを追加しようとしています。私はこのすべてに新しいので、私は自分自身にとってそれを難しくしている可能性があります。ここでコンテナコンポーネントからプレゼンテーションコンポーネントへのReact-passオブジェクト
は私のコンテナコンポーネントのコードです:
class TemplateContentContainer extends Component {
constructor() {
super()
this.fetchModule = this.fetchModule.bind(this)
this.removeModule = this.removeModule.bind(this)
this.renderModule = this.renderModule.bind(this)
}
componentWillReceiveProps(nextProps) {
if(nextProps.addAgain !== this.props.addAgain) // prevent infinite loop
this.fetchModule(nextProps.addedModule)
}
fetchModule(id) {
this.props.dispatch(actions.receiveModule(id))
}
renderModule(moduleId) {
let AddModule = "Modules.module" + moduleId
return <AddModule/>
}
removeModule(moduleRemoved) {
console.log('remove clicked' + moduleRemoved)
this.props.dispatch(actions.removeModule(moduleRemoved))
}
render() {
return (
<div>
<TemplateContent
addedModule={this.props.addedModule}
templateModules={this.props.templateModules}
removeModule={this.removeModule}
renderModule={this.renderModule}
/>
</div>
)
}
}
と表現的コンポーネントのコード:
const TemplateContent = (props) => {
let templateModules = props.templateModules.map((module, index) => (
<li key={index}>
{props.renderModule(module)}
<button onClick={props.removeModule.bind(this, index)}>
remove
</button>
</li>
))
return (
<div>
<ul>
{templateModules}
</ul>
</div>
)
}
renderModule
関数が返すオブジェクトが、それはプレゼンテーションコンポーネントに渡されていたときにもう動作しません(classNameとして渡されない限りオブジェクトを返します)
私はmoからモジュールをインポートしています私はindex.js
ファイル
import * as Modules from '../components/modules'
にそれらのすべてをエクスポートdulesフォルダは任意のヘルプは高く評価されるだろう、それは理にかなって願っています。
ありがとうございます!
私の子コンポーネントはどのように見えますか? – Majki
私は、コンテナコンポーネント内でモジュール= {this.renderModule(module)}を実行しても、私のチャイルドコンポーネント内のオブジェクトを挿入しないで、開発ツールでは正しいことがわかる それはコンポーネントを –
Majki
を通してプルしません私は私の答えを更新しました、うまくいけばこれは以前よりあなたの質問をキャプチャします。 – Spoke