Last active
February 15, 2020 18:52
-
-
Save moehammoud/564af998a9bb9f40e48d83f3297e792d to your computer and use it in GitHub Desktop.
React JS Cheatsheet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is my personal reference for React JS. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSON.stringify(this.state.data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.setState({ | |
arrayvar: this.state.arrayvar.concat([newelement]) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onClick={() => this.afunction(index)} | |
//afunction is a function name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(event){ | |
event.target.value | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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