Skip to content

Instantly share code, notes, and snippets.

@moehammoud
Last active February 15, 2020 18:52
Show Gist options
  • Save moehammoud/564af998a9bb9f40e48d83f3297e792d to your computer and use it in GitHub Desktop.
Save moehammoud/564af998a9bb9f40e48d83f3297e792d to your computer and use it in GitHub Desktop.
React JS Cheatsheet.
This is my personal reference for React JS.
JSON.stringify(this.state.data)
this.setState({
arrayvar: this.state.arrayvar.concat([newelement])
})
onClick={() => this.afunction(index)}
//afunction is a function name
handleRemove : function(index){
var newData = this.state.items.slice(); //copy array
newData.splice(index, 1); //remove element
this.setState({items: newData}); //update state
console.log(this.state.items[1])
index.currentTarget.style.backgroundColor = 'red';
}
var ThisComponent = React.creacteClass({
render: function(){
var listComponents = this.state.data.map((item,index) => {
<li key={id}> {item.arrayobject} </li>
});
return(
<div>
{listComponents}
</div>
);
}
});
// item,index has to be in that order
var ChildComponent = React.createClass({
render: function() {
return (
<div>
<button onClick={this.performMagicClick}>Do Magic</button>
</div>
);
}
});
var ParentComponent = React.createClass({
performMagic: function() {
alert('TAADAH!');
},
render: function() {
return (
<div>
<ChildComponent performMagicClick={this.performMagic}/>
</div>
);
}
});
ReactDOM.render(
<ParentComponent />,
document.getElementById('container')
);
function(event){
event.target.value
}
var StateComponent = React.createClass({
getInitialState: function() {
return {currentState: false};
},
handleClick : function() {
this.setState({currentState: !this.state.currentState});
},
render: function() {
return (
<div>
<h1> This is my current State : {this.state.currentState ? "Chill" : "Not Chill"}</h1>
<button type="button" className="btn btn-primary"
onClick={this.handleClick} >Change State</button>
</div>
)
}
});
aFunction : function(event){
event.target //You can reference any element using event.target
//ID name
event.target.id
//className
event.target.className
//Input value
event.target.value
}
testFunc : function(index){
var str=this.state.data;
str[index].title="Tokyo";
this.setState({data:str});
console.log(JSON.stringify(this.state.data))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment