在我的 React/Redux/ReactRouterV4 应用程序的一小部分中,我有以下组件层次结构,
- Exhibit (Parent)
-- ExhibitOne
-- ExhibitTwo
-- ExhibitThree
在 Exhibit 的子项中,也可以渲染大约 6 种不同的可能路线。别着急,我会用一些代码来解释。
这是我的父展览组件:
export class Exhibit extends Component {
render() {
const { match, backgroundImage } = this.props
return (
<div className="exhibit">
<Header />
<SecondaryHeader />
<div className="journey"
style={{
color: 'white',
backgroundImage: `url(${backgroundImage})`,
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center-center'
}}>
<Switch>
<Route path={`${match.url}/exhibit-one`} component={ExhibitOne} />
<Route path={`${match.url}/exhibit-two`} component={ExhibitTwo} />
<Route path={`${match.url}/exhibit-three`} component={ExhibitThree} />
<Redirect to="/" />
</Switch>
</div>
</div>
)
}
}
基本上,它的工作就是显示其中一个展品子组件,并设置背景图像。
这是子组件之一,ExhibitOne:
export default class ExhibitOne extends Component {
constructor(props) {
super(props)
}
render() {
const { match } = this.props
return (
<div className="exhibit-one">
<Switch>
<Route path={`${match.url}/wall-one`} component={ExhibitHOC(WallOne)} />
<Route path={`${match.url}/wall-two`} component={ExhibitHOC(WallTwo)} />
<Route path={`${match.url}/wall-three`} component={ExhibitHOC(WallThree)} />
<Route path={`${match.url}/wall-four`} component={ExhibitHOC(WallFour)} />
<Route path={`${match.url}/wall-five`} component={ExhibitHOC(WallFive)} />
<Route path={`${match.url}/wall-six`} component={ExhibitHOC(WallSix)} />
</Switch>
</div>
)
}
}
为了减少打字,我决定将组件包装在一个高阶组件中,其目的是调度一个动作,将在顶级 Exhibit 父组件上设置适当的背景图像。
这是高阶组件:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../../actions/wall-background-image'
export default function(ComposedComponent) {
class ExhibitHoc extends Component {
componentDidMount = () => this.props.setBackgroundImage(`./img/exhibit-one/${this.getWall()}/bg.jpg`)
getWall = () => {
// this part isnt important. it is a function that determines what wall I am on, in order to set
// the proper image.
}
render() {
return <ComposedComponent />
}
}
return connect(null, actions)(ExhibitHoc);
}
在 ExhibitOne 的初始加载时,通过查看控制台中的 Redux Logger,我可以看到 setBackgroundImage 操作创建者执行了两次。我最初倾向于使用 componentDidMount 是因为我认为使用它会限制动作创建者只执行一次。这是日志的屏幕截图:
我想我可能误解了高阶组件是如何工作的,或者它可能是某种 React Router V4 的东西?无论如何,对于为什么会执行两次,任何帮助将不胜感激。