47
loading...
This website collects cookies to deliver better user experience
Note: Apply it sparingly because it makes component reuse more difficult. Consider component composition as it is often a simpler solution than context.
One should limit its use because it can expose users to potential cross-site scripting attacks.
import LazyComponent from './LazyComponent';
const LazyComponent = React.lazy(() => import('./LazyComponent'));
import React from 'react';
const MyComponent = React.memo(props => {
/* render only if the props changed */
});
Note: If React.memo has a useState, useReducer or useContext Hook in its implementation, it will still re-render when state or context change.
function App() {
return (
<React.Fragment>
<h1>Best App</h1>
<p>Easy as pie!</p>
</React.Fragment>
);
}
function App() {
return (
<>
<h1>Best App</h1>
<p>Easy as pie!</p>
</>
);
}
const EnhancedComponent = higherOrderComponent(WrappedComponent);
const MyComponent = ({title, children}) => {
return (
<>
<h1>{title}</h1>
{children}
</>
);
}
import { MyComponent } from './MyComponent';
const App = () => {
return (
<MyComponent title=“Simple React App”>
<h2>Very Kewl Feature</h2>
</MyComponent>
);
}
<button onClick={() => this.handleClick(id)} />
const handleClick = (id) => () => {
console.log(`The id is ${id}`)
};
<button onClick={this.handleClick(id)} />
// Wrong
this.setState({
counter: this.state.counter + 1
})
// Correct
this.setState((prevState) => ({
counter: prevState.counter + 1
}))