【发布时间】:2019-09-01 17:58:25
【问题描述】:
我是 react hooks 的初学者,我也在我的项目中使用 graphql。有人可以帮助将组件转换为反应钩子。
class Detail extends Component {
static propTypes = {
classes: PropTypes.shape(commonStyles).isRequired,
siteId: PropTypes.string
};
state = {
showDialog: false
};
handleRowHistory = () => {
this.setState({ showDialog: true });
};
render() {
const { showDialog } = this.state;
const { data, classes, siteId } = this.props;
if (data.error) {
return <CardErrorIndicator apolloData={data} />;
} else if (data.loading) {
return <CardLoadingIndicator />;
}
const { sites } = data;
const { controller } = _.first(sites);
return (
<div
className={classNames(
classes.overall,
classes.basePadding,
"site-assets-detail-page"
)}
>
<SiteRowController
controller={controller}
onRowHistoryClick={this.handleRowHistory}
/>
{showDialog && (
<RowHistoryDialog
open={showDialog}
siteId={siteId}
onClose={() => this.setState({ showDialog: false })}
/>
)}
</div>
);
}
}
const DetailWithData = compose(
graphql(SITE_ASSETS_DETAIL_QUERY, {
options: props => ({
variables: {
siteId: props.siteId
},
pollInterval: 5000
})
})
)(Detail);
export default withStyles(commonStyles)(DetailWithData);
我知道 React-Hooks 是 React Class 风格的替代品。问题是我是否可以重构并在其中添加 React 钩子。
谢谢
【问题讨论】:
标签: reactjs react-hooks