0
我有一个使用反应 - 终极版connect
这个组件:转换使用连接到容器组件无状态组件 - 优化
let Vepo = (props) => (
<Container >
<Header style={styles.header}>
<Left>
<Button transparent>
</Button>
</Left>
<Body>
<Title style={styles.title}>Search</Title>
</Body>
<Right>
</Right>
</Header>
<Container style={styles.container}>
<ScrollView >
<Keywords />
<Categories />
</ScrollView>
</Container>
</Container>
)
Vepo = connect(
null,
null
)(Vepo)
export default Vepo
而且我只是为了方便的使用生命周期方法它转换成一个容器组件,而不connect
:
class Vepo extends Component {
componentDidMount() {
const { store } = this.context
this.unsubscribe = store.subscribe(() => {
this.forceUpdate()
console.log(store)
})
console.log(store)
}
componentWillUnmount() {
this.unsubscribe()
}
render() {
return(
<Container >
<Header style={styles.header}>
<Left>
<Button transparent>
</Button>
</Left>
<Body>
<Title style={styles.title}>Search</Title>
</Body>
<Right>
</Right>
</Header>
<Container style={styles.container}>
<ScrollView >
<Keywords />
<Categories />
</ScrollView>
</Container>
</Container>
)}
}
Vepo.contextTypes = {
store: React.PropTypes.object
}
export default Vepo
但是我刚刚从SO回答中看到,forceUpdate()
是不必要的破解。
我需要做什么componentDidMount
和componentWillUnmount
有一个高性能的组件?订阅是否必要?如果我只删除this.forceUpdate()
它会成为一个高性能组件吗?
谢谢,但如果是我提到我需要的生命周期方法? – BeniaminoBaggins
我已经更新了答案,并将它们添加回来。我删除了他们,因为他们不需要:)。将所有商店添加到一个组件中,如代码,并不是一个好主意。 –