Answer the question
In order to leave comments, you need to log in
Why is the handler not hung on the react component?
There is such a component
var React = require('react');
class Task extends React.Component {
constructor(props){
super(props);
this.state = {
isOpened:false
}
}
clickHandler(e){
e.preventDefault();
console.log('!!!');
this.setState({
isOpened: !this.state.isOpened
});
return false;
}
render(){
var description = this.state.isOpened?this.props.description:'';
return <a onClick={this.clickHandler.bind(this)} href="#" className="list-group-item list-group-item-action flex-column align-items-start">
<div className="d-flex w-100 justify-content-between">
<h5 className="mb-1">{this.props.name}</h5>
<small>3 days ago</small>
</div>
<p className="mb-1">{description}</p>
</a>
}
}
module.exports = Task;
Answer the question
In order to leave comments, you need to log in
And where did you get the idea that he should be there? I think you should tighten up the basics of JavaScript .
If you are using ES6 syntax , use imports and exports:
It is better to bind handlers not in render , but in the constructor:
constructor(props) {
super(props);
this.state = {
isOpened: false,
};
this.clickHandler = this.clickHandler.bind(this);
}
clickHandler = e => {
// your stuff
};
this.setState(prevState => ({
isOpened: !prevState.isOpened,
}));
When rendering on the server, regular html comes to the client, which knows nothing about handlers. To fix this, you need to write a script tag to the page that you send, which contains the code for attaching components, as if it were being rendered on the client.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question