Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrow function syntax in React stateless components

I've recently discovered the joy of stateless components. For instance, this makes me quite happy (and it works):

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
}

I would be even happier if I could make the same component work with E6 arrow function syntax, like so:

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
}

I've followed this video, but I can't get the arrow syntax version to work.

Can anybody point out what I'm doing wrong please?

like image 426
U r s u s Avatar asked Mar 16 '16 13:03

U r s u s


1 Answers

You're missing the export declaration. Add this to your module:

export {ClassroomsOverview as default}

I would however recommend using the export default syntax with the function declaration.

like image 158
Bergi Avatar answered Dec 12 '22 22:12

Bergi