我想重构使用'React.createClass'的代码并用JavaScript类替换。如何将Classes synthax与connect和redux结合起来?如何用JavaScript类编写代码?
import {connect} from 'react-redux';
const UserOverview= React.createClass({
...
});
const mapStateToProps = (state) => {
return {selectedUser: state.project.selectedUser};
} ;
export default connect(mapStateToProps, null)(UserOverview);
答案 0 :(得分:3)
它或多或少相同 - 您的Object属性只是成为类方法/属性。
class UserOverview extends React.Component {
// methods, etc - remember to watch out for scoping, especially in callbacks
onDivClick = () => {
// either use ES6 style "arrow functions" (like this)
// or rebind your callback handlers inside of `constructor`
}
render() {
return <div onClick={this.onDivClick}/>;
}
}
export default connect(mapStateToProps, {})(UserOverView);