Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A React component that shows a comparison of checks using no constants, with open checks of constants, and with abstracted constants
import React from 'react'
const teacher = 'teacher';
const student = 'student';
const isTeacher = (userType) => {
return userType === teacher;
const isStudent = (userType) => {
return userType === student;
const user = {
type: teacher,
name: 'Teacher Person'
export const Component = (props) => {
// No constants
{ (user.type === 'teacher')
? <div>Teacher view</div>
: <div>Student view</div>
// With open constants
{ (user.type === teacher)
? <div>Teacher view</div>
: <div>Student view</div>
// With abstracted constants
{ (isTeacher(user))
? <div>Teacher view</div>
: <div>Student view</div>
export default Component

This comment has been minimized.

Copy link

@ghost ghost commented Mar 15, 2018

For the abstracted constants example, shouldn’t the first part of the ternary be (isTeacher(user.type))?
Because otherwise isn’t it comparing the whole user object to teacher?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.