react a tag onclick code example
Example 1: react button onclick
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.sayHello = this.sayHello.bind(this);
}
sayHello() {
alert('Hello!');
}
return (
<button onClick={this.sayHello}>
Click me!
</button>
);
}
export default App;
Example 2: react onclick to make another button
import React from 'react';
const App = () => {
const message = () => {
console.log("Hello World!")
}
return (
<button onClick={message}> Press me to print a message! </button>
);
}
Example 3: onclick re
handleClick(id) {
this.setState({
vote_status: !this.state.vote_status,
})
let vote_object = {
voting_object: id,
post_id: this.props.postId
}
this.props.submitvote(vote_object)
//this.forceUpdate()
}
render() {
console.log("getVoteStatus", this.props.getVoteStatus)
let {contents, submitvote, postId, voted_id} = this.props
return (
<div className="txt_vote_bar_div" style={{
color: voted_id === contents.post_poll_content_id ? 'white' : '#9da0a4',
backgroundColor: this.state.vote_status ? '#0b97c4' : '#FFFFFF'
}} id={contents.post_poll_content_id}>
<p className="txt_vote_choice"
style={{color: this.state.vote_status || voted_id === contents.post_poll_content_id ? '#FFFFFF' : '#6a6a6a'}}
id={"id" + contents.post_poll_content_id}
onClick={() => {
this.handleClick(contents.post_poll_content_id);
}}> {contents.content} </p>
<p className="txt_tot_votes"
style={{color: this.state.vote_status || voted_id === contents.post_poll_content_id ? '#FFFFFF' : '#6a6a6a'}}> {contents.votes}%
(Votes:)</p>
</div>
);
};
Example 4: react button onclick
<button onClick={sayHello}>Click</button>