我最近发现了无状态组件的乐趣.例如,这让我很开心(并且它有效):
import { Component,PropTypes } from 'react'; export default function ClassroomsOverview(props,context) { return ( <div> <p>{context.classrooms.data.length} Classrooms.</p> <p>{context.classrooms.members.length} Students</p> </div> ); } ClassroomsOverview.contextTypes = { classrooms: PropTypes.object }
如果我可以使用E6 arrow function syntax使相同的组件工作,我会更高兴,就像这样:
import { Component,PropTypes } from 'react'; const ClassroomsOverview = (props,context) => ( <div> <p>{context.classrooms.data.length} Classrooms.</p> <p>{context.classrooms.members.length} Students</p> </div> ); ClassroomsOverview.contextTypes = { classrooms: PropTypes.object }
我已经遵循了this video,但我无法使用箭头语法版本.
任何人都可以指出我做错了吗?