Last active
March 15, 2018 15:05
-
-
Save 3ZsForInsomnia/20b8466ea64125e8b541cbd2ddeb2e37 to your computer and use it in GitHub Desktop.
A React component that shows a comparison of checks using no constants, with open checks of constants, and with abstracted constants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | |
return( | |
<div> | |
// 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> | |
} | |
</div> | |
) | |
} | |
export default Component |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?