export default class Data extends Component {
constructor(props)
{
super(props);
this.state = {
pages : [],
}
this.handleClick = this.handleClick.bind(this);
}
handleClick(event){
var newpages = this.state.pages;
newpages.push(event);
this.setState = ({
pages: newpages,
});
console.log(this.state.pages)
if(this.state.pages!=undefined)
{this.state.pages.forEach(element =>
((element!=undefined) ?console.log("jsjha"):console.log("hsdgahdhasgh") ))}
//working
}
render() {
console.log(this.state.pages); // not working, nothing is printed in the console
return (
<>
<button onClick={()=>this.handleClick("Tab 1")} value="Tab 1" id="1">Tab 1</button>
<button onClick={()=>this.handleClick("Tab 2")} value="Tab 2" id="2">Tab 2</button>
<button onClick={()=>this.handleClick("Tab 3")} value="Tab 3" id="3">Tab 3</button>
<h3>Hello this is Data bar!</h3>
<hr/>
{
(this.state.pages.map(function(element,i)
{return <p key={i}>{element}hdkashdak</p> }))
}
// not working
</>
);
}
}
In the console.log , I can see "jsjha" being printed, means the state is bein updated. But in the render function I cannot see the set being updated.
I have even bind the handleClick function.
Also, is everything correct witht the JSX map function? I am using the return statement and still it isn't working.