You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This repository is a cheat sheet to React for daily use. It contain a lot of snippets from my own use / official documentation and i'll improve it soon ! It's made for people like me who like to continue have a overview of some snippets.
State are locals and fully controlled by the component itself.
// Update state with setState, except in constructorthis.setState({title:'Updated title',});
// Set state with previous statethis.setState((prevState,props)=>{return{count:prevState.count+1};});
// Declare initial state in constructorclassHeadingextendsReact.Component{constructor(props){super(props);this.state={title:'My title'};}}
// Do not update state directlythis.state.title='Hello';
// Lifting state up to share state between componentclassWrapperextendsReact.Component{constructor(props){super(props);this.handleInputChange=this.handleInputChange.bind(this);this.state={value:''};}handleInputChange(value){this.setState({value});}render(){constvalue=this.state.value;return(<Inputvalue={value}onInputChange={this.handleInputChange}/>);}}classInputextendsReact.Component{constructor(props){super(props);this.handleChange=this.handleChange.bind(this);}handleChange(e){this.props.onInputChange(e.target.value);}render(){constvalue=this.props.value;return<inputvalue={value}onChange={this.handleChange}/>;}}
// React Event are in camelCase<buttononClick={handleClick}> Action</button>
// Use preventDefault instead of return falsefunctionhandleClick(e){e.preventDefault();}
// Bind this to use it in the callbackconstructor(props){super(props);this.handleClick=this.handleClick.bind(this);}
// Pass data to callback<buttononClick={(e)=>this.deleteItem(id,e)}>Delete item</button><buttononClick={this.deleteItem.bind(this,id)}>Deleteitem</button>
// Using if operator with propsfunctionHeading(props){constisHome=props.isHome;if(isHome){return<HomeHeading/>;}return<PageHeading/>;}
// Using if operator with staterender(){constisHome=this.state.isHome;letheading=null;if(isHome){heading=<HomeHeading/>;}else{heading=<PageHeading/>;}return(<div>{heading}</div>);}
// Using ternary operator<div>{isHome ?<HomeHeading/> :<PageHeading/>}</div>
// Using logical operator<div>{messages.length>0&&<h1> You have messages</h1>}</div>
// Prevent component from renderingfunctionModal(props){if(!props.isShow){returnnull;}return(<div> Modal</div>);}
// In controlled component, each state mutation have an handler functionclassFormextendsReact.Component{constructor(props){super(props);this.state={value:''};this.handleChange=this.handleChange.bind(this);this.handleSubmit=this.handleSubmit.bind(this);}handleChange(e){this.setState({value:e.target.value});}handleSubmit(e){alert('Submitted value: '+this.state.value);e.preventDefault();}render(){return(<formonSubmit={this.handleSubmit}><inputtype="text"value={this.state.value}onChange={this.handleChange}/><inputtype="submit"value="Submit"/></form>);}}
// Force to uppercase in handlerhandleChange(e){this.setState({value:e.target.value.toUpperCase()});}
// <textarea> in React use a value attribute<textareavalue={this.state.value}onChange={this.handleChange}/>
// <select> use a value and not a selected attribute<selectvalue={this.state.value}onChange={this.handleChange}><optionvalue="a">Option A</option><optionvalue="b">Option B</option></select>
// <select value can have an array for multiple values<selectmultiple={true}value={['a','b']}>
// Handle multiple inputs with name attributehandleInputChange(e){consttarget=e.target;constvalue=target.value;constname=target.name;this.setState({[name]:value});}render(){return(<form><inputname="firstName"onChange={this.handleInputChange}/><inputname="lastName"onChange={this.handleInputChange}/></form>);}
// This two elements are similar :constelement=(<h1className="heading"> Hello!</h1>);constelement=React.createElement('h1',{className:'heading'},'Hello!');
// Use componentDidMount hook with fetchclassPostsListextendsComponent{constructor(props){super(props);this.state={posts:[]};}componentDidMount(){fetch('https://example.com/posts').then(response=>response.json()).then(data=>this.setState({posts:data.posts}));.catch(error=>console.log(error));}}
// Use Axios library to fetch datasimportaxiosfrom'axios';componentDidMount(){axios.get('/post',{params:{ID:123}}).then(function(response){console.log(response);}).catch(function(error){console.log(error);});}