我想要一点代码智慧,而不是修复。我以一种方式学习了react,并且我看到了不同格式的代码。我在试着了解它们之间的区别。
我得到了这个jsx代码片段:
import React from 'react'
import { Route, Redirect } from 'react-router'
import { connect } from 'react-redux'
import * as reducers from '../reducers'
const PrivateRoute = ({ component: Component, isAuthenticated, ...rest }) => (
<Route {...rest} render={props => (
isAuthenticated ? (
<Component {...props}/>
) : (
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}}/>
)
)}/>
);
const mapStateToProps = (state) => ({
isAuthenticated: reducers.isAuthenticated(state)
});
export default connect(mapStateToProps, null)(PrivateRoute);
代码运行得很好,但我正在尝试理解它。我学到的课程是以班级为基础的。
例如:
class Logout extends Component {
componentDidMount() {
this.props.dispatch(logout());
}
render() {
return(<Redirect to="/" push={true} />);
}
}
关于这段代码,我有两个问题:
这看起来是一个函数,是真的吗?,,
,
如果代码应该是一个类,并且我可以看到它应该是什么样子,那么我就知道如何将其余的代码转换为正确的代码。
谢谢
https://stackoverflow.com/questions/50778377
复制相似问题