constructor(props) {
super(props);
this.state = {
pokeDex: []
};
}
function add(x = 1, y = 2) {
return x + y;
}
add();

Explanation: function that called without parameter will use its param default value, thus x will always be default to 1 and y will always be default to 2.
const { tree, lake } = nature;
ReactDom.render(
<Message sent=false />,
document.getElementById("root")
);
<Message sent={false} />,
ReactDom.render(<Message sent="false" />, document.getElementById('root'));
<Message sent="false" />,
ReactDom.render(<Message sent="false" />, document.getElementById('root'));
const PokeDex = (props) => {
const [pokeDex, setPokeDex] = useState([]);
/// ...
};
Explanation: useState always return an array with two values, the state itself (on first value) and the set function that lets you update the state (on second value) useState Reference
const Greeting = ({ initName }) => {
const greet = (name) => console.log("Hello, " + name + "!");
return <button onClick={ ... }>Greeting Button </button>
}
Explanation: Apparently the question misstyped greet as hug. Putting this aside, we can still learn from this question.
this. In a browser window the global object is [object Window].
This is a functional Component, so this from this.hug actually refers to browser window.
Since it is a functional component, we can directly refer to hug without using this.initName is available in Greeting’s function scope, so we can directly supply as an argument to hug().React Hooks useCallback docuementation
useRef hook?class Greeting extends React.Component {
render() {
return <h1>Hello {this.props.name}!<h1>;
}
}
const Greeting = (name) => <h1>{name}</h1>function Greeting(name){return <h1>{name}</h1>;}const Greeting = props => { <h1>{props.name}</h1> }const Greeting = ({ name }) => <h1>Hello {name}</h1>;waitlist not updating correctly?const Waitlist = () => {
const [name, setName] = useState('');
const [waitlist, setWaitlist] = useState([]);
const onSubmit = (e) => {
e.preventDefault();
waitlist.push(name);
};
return (
<div>
<form onSubmit={onSubmit}>
<label>
Name: <input type="text" value={name} onChange={(e) => setName(e.target.value)} />
</label>
<button type="submit">Add to waitlist</button>
</form>
<ol>
{waitlist.map((name) => (
<li key={name}>{name}</li>
))}
</ol>
</div>
);
};
waitlist is being mutated directly. Use the setWaitlist function instead to update the waitlist state.Add to waitlist is clicked.Add to waitlist button is missing a click handler.waitlist array.import React from 'react';
const [poked, setPoked] = React.useState(false);
function PokeButton() {
return <button onClick={() => setPoked(true)}>{poked ? 'You have left a poke.' : 'Poke'}</button>;
}
function PokeButton() {
const { poked, setPoked } = useState(false);
return <button onclick={() => setPoked(true)}>{poked ? 'You have left a poke.' : 'Poke'}</button>;
}
const OtherComponent = React.lazy(() => import('./OtherComponent.js'));
function MyComponent() {
return (
<XXXX fallback={<spinner />}>
<OtherComponent />
</XXXX>
);
}
useEffect(callNetworkFunc, XXXX);
<div>{isLoggedIn ? <Hello /> : null}</div>
useLayoutEffect?fetch() function come from?fetch() is supported by most browsers.key prop when rendering a list of componentskey prop is used to provide a unique identifier for the component.key prop is used to define the color of the component.key prop is required to render a list of components.key prop is used by React to optimize updates and identify which items have changed or been added/removed in the list.React.createComponent()function Component() or class Component extends React.Componentnew React.Component()React.makeComponent()Reference Components and Props
useEffect hook?Reference Using the Effect Hook
statepropscontextref<ChildComponent name="John" age={25} />
Reference Components and Props
function Button() {
const handleClick = () => {
console.log('Button clicked');
};
return <button onClick={handleClick}>Click me</button>;
}
onclick instead of onClickaddEventListener methodcomponentWillMountcomponentDidMountcomponentWillUpdatecomponentDidUpdatethis.state.count = 1;this.setState({ count: 1 });this.updateState({ count: 1 });this.state = { count: 1 };return (
<React.Fragment>
<h1>Title</h1>
<p>Description</p>
</React.Fragment>
);
event.stop()event.preventDefault()event.cancel()event.halt()function Greeting({ isLoggedIn }) {
return <div>{isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}</div>;
}
if-else statements inside JSXswitch statements inside JSXReference Conditional Rendering
useCallbackuseMemouseEffectuseStateuseCallback hook?function Parent() {
const handleClick = () => console.log('Clicked');
return <Child onClick={handleClick} />;
}
onClick="handleClick"onClick={handleClick()}Reference Passing Functions to Components
Reference Components and Props
useStateuseReduceruseEffectuseContextfunction Form() {
const [value, setValue] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
console.log(value);
};
return (
<form onSubmit={handleSubmit}>
<input value={value} onChange={(e) => setValue(e.target.value)} />
<button type="submit">Submit</button>
</form>
);
}
setCount((prevCount) => prevCount + 1);
this.setState with previous stateuseStateuseRefuseEffectuseContextuseEffect(() => {
const fetchData = async () => {
const response = await fetch('/api/data');
const data = await response.json();
setData(data);
};
fetchData();
}, []);
Reference Effect Hook with Async
function useCounter(initialValue = 0) {
const [count, setCount] = useState(initialValue);
const increment = () => setCount(count + 1);
const decrement = () => setCount(count - 1);
return { count, increment, decrement };
}
createHook methodReference Building Your Own Hooks
useLayoutEffect hook?Reference useLayoutEffect Hook
shouldComponentUpdate onlyReact.memo, useMemo, useCallback, and shouldComponentUpdatecomponentDidUpdate onlyReference Optimizing Performance
const props = { name: 'John', age: 25, city: 'New York' };
return <UserCard {...props} />;
Reference JSX Spread Attributes
useEffectuseDebugValueuseStateuseRefuseEffect(() => {
const timer = setInterval(() => {
console.log('Timer tick');
}, 1000);
return () => {
clearInterval(timer);
};
}, []);
React.createRefReact.forwardRefReact.passRefReact.sendRefuseImperativeHandle hook?