Tell us what’s happening:
I am having trouble understanding the instructions and the code. I peeked at the solution and I don’t understand what it all means. By “all” I mean the instructions, code, and solutions to this challenge and all prior challenges in the React section. So I thought I should ask for some clarification.
Specifically:
- What is a “method” in React? Is it the same as a “method” in JS?
- What is “class” in React? Is it the same as “class” in JS?
- What is “extends React.Component?”
- What is “constructor(props)” and “super(props)”?
- What are “props?”
- What does it mean to “bind” or be “bound to”?
- What does the “this” keyword mean? (I’ve been working on this one for a few months now without generating much understanding)
- What is “state?”
- What is a React “component?”
- What does “render to the page” mean?
- What does it mean when a “component” “updates”?
I suppose I might have other questions as well, but these are the ones that come to mind right now.
thanks for reading my questions…
Your code so far
const inputStyle = {
width: 235,
margin: 5
}
class MagicEightBall extends React.Component {
constructor(props) {
super(props);
this.state = {
userInput: '',
randomIndex: ''
}
this.ask = this.ask.bind(this);
this.handleChange = this.handleChange.bind(this);
}
ask() {
if (this.state.userInput) {
this.setState({
randomIndex: Math.floor(Math.random() * 20),
userInput: ''
});
}
}
handleChange(event) {
this.setState({
userInput: event.target.value
});
}
render() {
const possibleAnswers = [
'It is certain',
'It is decidedly so',
'Without a doubt',
'Yes, definitely',
'You may rely on it',
'As I see it, yes',
'Outlook good',
'Yes',
'Signs point to yes',
'Reply hazy try again',
'Ask again later',
'Better not tell you now',
'Cannot predict now',
'Concentrate and ask again',
'Don\'t count on it',
'My reply is no',
'My sources say no',
'Most likely',
'Outlook not so good',
'Very doubtful'
];
const answer = this.randomIndex // << change code here
return (
<div>
<input
type="text"
value={this.state.userInput}
onChange={this.handleChange}
style={inputStyle} /><br />
<button onClick={this.ask}>
Ask the Magic Eight Ball!
</button><br />
<h3>Answer:</h3>
<p>
{ /* change code below this line */ }
{this.answer}
{ /* change code above this line */ }
</p>
</div>
);
}
};
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
.
Challenge: Use Advanced JavaScript in React Render Method
Link to the challenge: