Ответ 1
Вы можете разбить старый компонент на два частичных компонента: один, который содержит состояние и обрабатывает события, и чистый, который просто отображает результаты. Не забудьте передать обработчики событий как обратные вызовы дочернему компоненту. Также обратите внимание, как родительский компонент использует возвращаемое значение функции createContainer()
.
// Parent component, setState should go here
export default class StateHolder extends React.Component {
constructor(params) {
super(params);
this.state = {myKey: 1};
}
incrementKey() {
this.setState({myKey: this.state.myKey + 1});
}
render() {
return <Container myKey={this.state.myKey} onClick={this.incrementKey.bind(this)} />;
}
}
// Child component, renders only
class PureComponent extends React.Component {
render() {
return <div>
{this.props.myValue}
<button onClick={this.props.onClick}>click me</button>
</div>;
}
}
// Decorated child container.
// Make sure to use this one in parent component render() function!
let Container = createContainer((props) => {
let doc = MyCollection.findOne({someKey: props.myKey});
return {
myValue: doc ? doc.someValue : null
}
}, PureComponent);