This file contains 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
// index.jsx | |
export default function ParentComponent(props) { | |
return ( | |
<div> | |
<SomeComponent someProp={props.somePropValue} | |
<div> | |
<AnotherComponent someOtherProp={props.someOtherPropValue} /> | |
</div> | |
</div> | |
) |
This file contains 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
// Don't do this! | |
function Component(props) { | |
return <AnotherComponent onChange={() => props.callback(props.id)} /> | |
} | |
// Do this instead :) | |
function Component(props) { | |
const handleChange = useCallback(() => props.callback(props.id), [props.id]); | |
return <AnotherComponent onChange={handleChange} /> | |
} |
This file contains 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
// ./Tooltip.jsx | |
const MUITooltip = React.lazy(() => import('@material-ui/core/Tooltip')); | |
function Tooltip({ children, title }) { | |
return ( | |
<React.Suspense fallback={children}> | |
<MUITooltip title={title}> | |
{children} | |
</MUITooltip> | |
</React.Suspense> | |
); |
This file contains 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
// Avoid this is the components are too "heavy" to mount/unmount | |
function Component(props) { | |
const [view, setView] = useState('view1'); | |
return view === 'view1' ? <SomeComponent /> : <AnotherComponent /> | |
} | |
// Do this instead if you' re opting for speed & performance gains | |
const visibleStyles = { opacity: 1 }; | |
const hiddenStyles = { opacity: 0 }; | |
function Component(props) { |
This file contains 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
// Don't do this! | |
function Component(props) { | |
const aProp = { someProp: 'someValue' } | |
return <AnotherComponent style={{ margin: 0 }} aProp={aProp} /> | |
} | |
// Do this instead :) | |
const styles = { margin: 0 }; | |
function Component(props) { | |
const aProp = { someProp: 'someValue' } |
This file contains 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
// don't do this! | |
function Component(props) { | |
const someProp = heavyCalculation(props.item); | |
return <AnotherComponent someProp={someProp} /> | |
} | |
// do this instead. Now `someProp` will be recalculated | |
// only when `props.item` changes | |
function Component(props) { | |
const someProp = useMemo(() => heavyCalculation(props.item), [props.item]); |
This file contains 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
// selectors.js | |
export const getItems = state => state.items; | |
// Component.jsx | |
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import { getItems } from './selectors'; | |
function MyComponent({ items }) { | |
return <div>{items.length}</div> |
This file contains 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
// actions.js | |
export const fireAction = item => ({ type: 'ACTION', payload: { item } }); | |
// Component.jsx | |
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import { getItems } from './selectors'; | |
function MyComponent({ item, fireActionWithItem }) { | |
return <button onClick={fireActionWithItem}>{item}</button> |
This file contains 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
// actions.js | |
export const fireAction = item => ({ type: 'ACTION', payload: { item } }); | |
// ExpensiveComponent.jsx | |
import React, { memo } from 'react'; | |
function ExpensiveComponent({ onClick }) { | |
return ( |
This file contains 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
// action.js | |
const fireAction = item => ({ type: 'ACTION', payload: { item } }); | |
// Option 1 | |
import React, { useCallback } from 'react'; | |
import { connect } from 'react-redux'; | |
import { getItems } from './selectors'; | |
function MyComponent({ item, fireAction, randomProp }) { | |
const fireActionWithItem = useCallback(() => fireAction(item), [item]); | |
return ( |
OlderNewer