-
Notifications
You must be signed in to change notification settings - Fork 0
Barney commentary #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,30 @@ | ||
| import React from "react"; | ||
|
|
||
| class Answer extends React.Component{ | ||
| constructor(props) { | ||
| super(props); | ||
| const Answer = props => ( | ||
| <form> | ||
| {props.options.map((option,i) => ( | ||
| <div className={`option${i}`} key={i}> | ||
| <input | ||
| type="radio" | ||
| name='rad' | ||
| id={`option_${i}`} | ||
| onChange={() => { | ||
| props.handleRadio(i) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer using anonymous event handlers in the view and calling function references within that – this avoids the necessity of re-binding class methods elsewhere because we explicitly avoid the |
||
| }} | ||
| /> | ||
| <label htmlFor={`option_${i}`}> | ||
| {option} | ||
| </label> | ||
| </div> | ||
| ))} | ||
|
|
||
| } | ||
| <input | ||
| type="submit" | ||
| className='waves-effect waves-light btn-large submit-button visible' | ||
| onClick={props.onClickCallback} | ||
| disabled={!props.selectionMade} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boolean attribute expressions can be expressed like this – I love it. |
||
| /> | ||
| </form> | ||
| ); | ||
|
|
||
| render() { | ||
|
|
||
| const options = this.props.options.map( (option,i) => { | ||
| return <div className={`option${i}`} key={i}> | ||
| <input type="radio" name='rad' id={`option_${i}`} onChange={this.props.handleRadio.bind(this, i)} /> | ||
| <label htmlFor={`option_${i}`} > {option} </label> | ||
| </div> | ||
| }); | ||
|
|
||
| return( | ||
| <form onSubmit={this.props.onClickCallback}> | ||
| {options} | ||
| <input | ||
| type="submit" | ||
| className='waves-effect waves-light btn-large submit-button visible' | ||
| onClick={this.props.onClickCallback} | ||
| disabled={ this.props.selectionMade ? '' : 'disabled'} /> | ||
| </form> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export default Answer; | ||
| export default Answer; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,12 @@ | ||
| import React from "react"; | ||
|
|
||
| const Question = (props) =>{ | ||
|
|
||
| const QuestionObejct = props.quiz[props.index]; | ||
|
|
||
| return( | ||
| <div className="questionWrapper" > | ||
| <div className='questions'> | ||
| {QuestionObejct.question} | ||
| </div> | ||
| const Question = ({question}) => ( | ||
| <div className="questionWrapper" > | ||
| <div className='questions'> | ||
| {question} | ||
| </div> | ||
| ) | ||
| } | ||
| </div> | ||
| ); | ||
|
|
||
|
|
||
| export default Question; | ||
| export default Question; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where possible, functional components are IMO better because you get an immediate assurance that there is no lifecycle and no state, which makes it easier to reason about contents.
Un-bracketed arrow functions are even better because they are 'point-free', ie there are no internal references, just a return expression. Previously there were 2 points: declare a variable to define the
options, then return a block of content which references theoptions. I prefer this because it's less moving parts to remember – some people dislike it on the grounds it encourages that any logic (ie theoptions.map) gets put in the content (I don't see a problem with that 🤷♂️ ).