有用なReact Higher-Order-Component(HOC)の例はありませんが、存在するシンプルなものはあまり単純ではありません。 HOCを使用して行うことができるかできないか。とにかく、私は見つけることができるものから、私は、次を試してみましたが、私はこれがHOCのを試してみて、使用するために私の最初の試みですので、私は(おそらく明白な)何かをmssingています確信しています:React HOCにラップされたコンポーネントが表示されない
HOC:
import { Component, PropTypes } from 'react'
export const WizardWrapper = Step => class extends Component {
constructor(props) {
super(props)
this.nextPage = this.nextPage.bind(this)
this.previousPage = this.previousPage.bind(this)
this.state = {
page: 1
}
}
nextPage() {
this.setState({ page: this.state.page + 1 })
}
previousPage() {
this.setState({ page: this.state.page - 1 })
}
goToPage(page) {
this.setState({ page })
}
render() {
const { onSubmit, submitButtonText, step} = this.props
let hasNext = this.props.step > this.state.page
let hasPrev = this.state.page < this.props.step
let nextText = hasNext ? 'Next' : submitButtonText
return (
<Step previousPage={hasPrev ? this.previousPage : null}
onSubmit={hasNext ? this.nextPage : onSubmit}
{...this.state} {...this.props}/>
)
}
}
一部コンポーネント:(それは実際にはまだ何もしていません包む場合でも)包まれたコンポーネントを表示する
export const WizardPage = (props) => <h1>Some page</h1>
試み:
import { Component, PropTypes } from 'react'
import {WizardWrapper} from 'common/client/components/wizard/WizardWrapper'
import {WizardPage} from 'common/client/components/wizard/WizardPage'
export default class FormTest extends Component {
render() {
let WrapperPage = WizardWrapper(WizardPage)
return (
<div>
{WrapperPage}
</div>
)
}
}
エラーはありませんが、何も表示されません。私が見逃していることや間違っていることは何ですか?
TIA!
私はこれをテストしたところ、うまく動作します。欠けているかもしれない唯一のことは、グローバルなことが起こっていない限り、コンポーネント内の 'import React'です。文字通り、私はあなたの正確なコードをコピーして貼り付けました。 – azium
ありがとう - しかし、今私は本当になぜそれが私のためではないのか困惑しています。私もメテオを使っているので、リアクションは私にとっては疑似グローバルです。 OK私はもう少し遊んで、私がそれを理解できるかどうかを見ます。返信いただきありがとうございます! –