var SayHello = createReactClass({
getInitialState: function() {
return {message: 'Hello!'};
},
handleClick: function() {
alert(this.state.message);
},
render: function() {
return (
<button onClick={this.handleClick}>
Say hello
</button>
);
}
});
class SayHello extends React.Component{
constructor(props) {
super(props);
this.state = {message: 'Hello!'};
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
console.log(this.state.message);
}
render() {
return (
<button onClick={this.handleClick}>
Say hello
</button>
);
}
}
class SayHello extends React.Component {
state = { message: 'Hello!' }
handleClick = () => {
console.log(this.state.message);
}
render() {
return (
<button onClick={this.handleClick}>
Say hello
</button>
);
}
}
const { useState } = React;
function SayHello() {
const [message, setMessage] = useState('Hello!'); //A 'hook'
const handleClick = () => {
setMessage('hi')
};
return (
<>
<p>{message}</p>
<button onClick={handleClick}>
Change Message
</button>
</>
);
}