【发布时间】:2019-08-23 05:37:32
【问题描述】:
我的公司正在使用 recompose 作为我们的状态管理工具。我们正在重构我们的应用程序以使用钩子。对于下面的代码,如何将 recompose 组件替换为 react hook 组件?
我理解withState变成useState,如:
withState('something', 'setSomething', null)
变成
const [something, setSomething] = useState(null);
withProps、withHandlers、compose、hoistStatics 和 lifecycle 会变成什么?
mapStateToProps 和 mapDispatchToProps 将如何工作?
import { compose, hoistStatics, withHandlers, withState, withProps, lifecycle } from 'recompose';
import { connect } from 'react-redux'
import myComponent from './myComponent'
const mapStateToProps = (state, props) => {
return {
}
};
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({
}, dispatch)
};
const enhancer = compose(
connect(mapStateToProps,mapDispatchToProps),
withProps(props => ({
myProp: props.myProp,
})),
withState('something', 'setSomething', null),
withState('somethingElse', 'setSomethingElse', null),
withHandlers({
myFunction: () => () => {
console.log(`I need help`);
}
}),
lifecycle({
componentDidMount() {
},
componentDidUpdate() {
}
})
);
export default hoistStatics(enhancer)(myComponent);
【问题讨论】:
标签: javascript reactjs react-native react-hooks recompose