Skip to content

Instantly share code, notes, and snippets.

@3ZsForInsomnia
Last active March 15, 2018 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3ZsForInsomnia/20b8466ea64125e8b541cbd2ddeb2e37 to your computer and use it in GitHub Desktop.
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
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
Copy link

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