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
In JSX, you can't use the word class! You have to use className instead
This is because JSX gets translated into JavaScript, and class is a reserved word in JavaScript
Self-closing tags
Fine in JSX: <br />
NOT FINE AT ALL in JSX: <br>
Event listeners
functionmakeDoggy(e){// Call this extremely useful function on an <img>.// The <img> will become a picture of a doggy.e.target.setAttribute('src','https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-puppy.jpeg');e.target.setAttribute('alt','doggy');}varkitty=(<imgsrc="https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-kitty.jpg"alt="kitty"onClick={makeDoggy}/>);ReactDOM.render(kitty,document.getElementById('app'));
Conditionals ?:
if-else statements don't work inside JSX, you probably want to make use of a ternary expression
varfiftyFifty=Math.random()<0.5;varTonightsPlan=React.createClass({name: 'Petr',render: function(){returnfiftyFifty ? <h1>My name is {this.name} and tonight I'm going out WOOO</h1> : <h1> My name is {this.name} and tonight I'm going to bed WOOO</h1>;}});ReactDOM.render(<TonightsPlan/>,document.getElementById('app'));
If a component has more than one child between its JSX tags, then this.props.children will return those children in an array. However, if a component has only one child, then this.props.children will return the single child, not wrapped in an array.
varApp=React.createClass({render: function(){return(<div><Listtype='Living Musician'><li>Sachiko M</li><li>Harvey Sid Fisher</li></List><Listtype='Living Cat Musician'><li>Nora the Piano Cat</li></List></div>);}});varList=React.createClass({render: function(){vartitleText='Favorite '+this.props.type;if(this.props.childreninstanceofArray){titleText+='s';}return(<div><h1>{titleText}</h1><ul>{this.props.children}</ul></div>);}});ReactDOM.render(<App/>,document.getElementById('app'));
You can call this.setState from any property passed to React.createClass... with one big exception. You can't call this.setState from the render function!
Any time that you call this.setState, this.setState AUTOMATICALLY calls render as soon as the state has changed.
That is why you can't call this.setState from inside of the render function! this.setState automatically calls render. If render calls this.setState, you will create an infinite loop.
UNIT 4: STATELESS COMPONENTS INHERIT FROM STATEFUL COMPONENTS
props vs state
Props are immutable!
A React component should use props to store information that can be changed, but can only be changed by a different component. A React component should use state to store information that the component itself can change.
If a Component needs to alter one of its attributes at some point in time, that attribute should be part of its state, otherwise it should just be a prop for that Component.
varChild=React.createClass({handleChange: function(e){varname=e.target.value;this.props.onChange(name);},render: function(){return(<div><selectid="great-names"onChange={this.handleChange}><optionvalue="Frarthur">Frarthur</option><optionvalue="Gromulus">Gromulus</option><optionvalue="Thinkpiece">Thinkpiece</option></select></div>);}});varSibling=React.createClass({render: function(){varname=this.props.name;return(<div><h1>Hey, my name is {name}!</h1><h2>Dont you think {name} is the prettiest name ever?</h2><h2>Sure am glad that my parents picked {name}!</h2></div>);}});varParent=React.createClass({getInitialState: function(){return{name: 'Frarthur'};},changeName: function(newName){this.setState({name: newName});},render: function(){return(<div><ChildonChange={this.changeName}/><Siblingname={this.state.name}/></div>);}});ReactDOM.render(<Parent/>,document.getElementById('app'));
UNIT 5: ADVANCED REACT
Inline styles
In regular JavaScript, style names are written in hyphenated-lowercase
In React, those same names are instead written in camelCase
In React, if you write a style value as a number, then the unit "px" is assumed
varstyleMe=<h1style={{background: 'lightblue',color: 'darkred'}}>Please style me! I am so bland!</h1>;ReactDOM.render(styleMe,document.getElementById('app'));
Styles in a variable
Defining a variable named style in the top-level scope would be an extremely bad idea in many JavaScript environments! In React, however, it's totally fine.
Remember that every file is invisible to every other file, except for what you choose to expose via module.exports. You could have 100 different files, all with global variables named style, and there could be no conflicts.
In this programming pattern, the container component does the work of figuring out what to display. The presentational component does the work of actually displaying it.
A container does data fetching and then renders its corresponding sub-component. That’s it.
// Normal way to display a prop:varMyComponentClass=React.createClass({render: function(){return<h1>{this.props.title}</h1>;}});// Stateless functional component way to display a prop:functionMyComponentClass(props){return<h1>{props.title}</h1>;}
An uncontrolled component is a component that maintains its own internal state. A controlled component is a component that does not maintain any internal state. Since a controlled component has no state, it must be controlled by someone else.
The fact that <input /> keeps track of information makes it an uncontrolled component.
In React, when you give an <input /> a value attribute, then something strange happens: the <input /> BECOMES controlled. It stops using its internal storage.
Lifecycle methods are methods that get called at certain moments in a component's life.
There are three categories of lifecycle methods: mounting, updating, and unmounting.
Mounting lifecycle methods
There are three mounting lifecycle methods componentWillMount, render, componentDidMount.
When a component mounts, it automatically calls these three methods, in order.
Mounting lifecycle events only execute the first time that a component renders.
If your React app uses AJAX to fetch initial data from an API, then componentDidMount is the place to make that AJAX call.
varFlashy=React.createClass({componentWillMount: function(){alert('AND NOW, FOR THE FIRST TIME EVER... FLASHY!!!!');},componentDidMount: function(){alert('YOU JUST WITNESSED THE DEBUT OF... FLASHY!!!!!!!');},render: function(){alert('Flashy is rendering!');return<h1>OOH LA LA LOOK AT ME I AM THE FLASHIEST</h1>;}});
Updating lifecycle methods
The first time that a component instance renders, it does not update. A component updates every time that it renders, starting with the second render.
There are five updating lifecycle methods componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate, render, componentDidUpdate.
Whenever a component instance updates, it automatically calls all five of these methods, in order.
shouldComponentUpdate: function(nextProps,nextState){if((this.props.text==nextProps.text)&&(this.state.subtext==nextState.subtext)){alert("Props and state haven't changed, so I'm not gonna update!");returnfalse;}else{alert("Okay fine I will update.")returntrue;}}
componentWillUpdate: function(nextProps,nextState){alert('Component is about to update! Any second now!');}
componentDidUpdate: function(prevProps,prevState){alert('Component is done rendering!');}
Unmounting lifecycle methods
A component's unmounting period occurs when the component is removed from the DOM. This could happen if the DOM is rerendered without the component, or if the user navigates to a different website or closes their web browser.
There is only one unmounting lifecycle method componentWillUnmount