次の2つの同等のパフォーマンスが賢明ですか?最初の例ではMyComponent
の小道具が更新されるたびにComponent1
とComponent2
が再レンダリングされますか?props/stateを使用してrender()にコンポーネントを表示するかどうかを選択すると、毎回再描画されますか?
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { selectShowComponent1, selectShowComponent2 } from './selectors';
@connect(createStructuredSelector({
showComponent1: selectShowComponent1,
showComponent2: selectShowComponent2,
}))
class MyComponent extends React.PureComponent {
static propTypes = {
showComponent1: React.PropTypes.bool,
showComponent2: React.PropTypes.bool,
};
render() {
const { showComponent1, showComponent2 } = this.props;
return (
<div>
{showComponent1 ? (<Component1>shown 1</Component1>) : ''}
{showComponent2 ? (<Component2>shown 2</Component2>) : ''}
</div>
);
}
}
export default MyComponent;
import React from 'react';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { selectShowComponent1, selectShowComponent2 } from './selectors';
@connect(createStructuredSelector({
showComponent1: selectShowComponent1,
showComponent2: selectShowComponent2,
}))
class MyComponent extends React.PureComponent {
static propTypes = {
showComponent1: React.PropTypes.bool,
showComponent2: React.PropTypes.bool,
};
render() {
const { showComponent1, showComponent2 } = this.props;
return (
<div>
<Component1 renderMe={showComponent1}>shown 1</Component1>
<Component2 renderMe={showComponent2}>shown 2</Component2>
</div>
);
}
}
export default MyComponent;
第2のアプローチは、両方のコンポーネントをレンダリングし、それらのコンポーネントの内側で表示するものを管理します。一方、最初のアプローチは、あなたの小道具で何かが変わるたびに新しいコンポーネントをマウントします。 – Kejt