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
function mapDispatchToProps(dispatch) { | |
return { | |
doSomething: () => { | |
dispatch(somethingDispatchable()); | |
}, | |
}; | |
} | |
class SomeComponent extends React.Component { | |
componentDidMount() { |
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
// Example of how `mapDispatchToProps` makes testing more straightforward. | |
test('SomeComponent should do something correctly', () => { | |
const mockedDoSomething = jest.fn(); | |
const component = mount(<SomeComponent doSomething={mockedDoSomething} />); | |
// Interact with the component to trigger the callback. In this case it's done on mount, | |
// but here you would simulate click events if it's attached to a button for example. | |
expect(mockedDoSomething).toHaveBeenCalled(); | |
// Other verifications. | |
}); |
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
const Component = () => { | |
const [ids] = useArrayIds(); | |
const [storedIds, setStoredIds] = useState(ids); | |
const removeId = useCallback((idToRemove) => { | |
setStoredIds((oldIds) => oldIds.filter((oldId) => oldId !== idToRemove)); | |
}, []); | |
useEffect(() => { | |
setStoredIds(ids); |
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
const useIdsHandler = (ids) => { | |
const [storedIds, setStoredIds] = useState(ids); | |
const removeId = useCallback((idToRemove) => { | |
setStoredIds((oldIds) => oldIds.filter((oldId) => oldId !== idToRemove)); | |
}, []); | |
const resetIds = useCallback((newIds) => { | |
setStoredIds(newIds); | |
}, []); |
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
const useIdsHandler = () => { | |
const [ids] = useArrayIds(); | |
const [storedIds, setStoredIds] = useState(ids); | |
useEffect(() => { | |
setStoredIds(ids); | |
}, [ids]); | |
const removeId = useCallback((idToRemove) => { |
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
const Component = () => { | |
const [showImage, setShowImage] = useState(true); | |
const hideImage = useCallback(() => { | |
if (showImage) { | |
setShowImage(false); | |
} | |
}, [showImage]); | |
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
const Component = () => { | |
const [images, setImages] = useState([]); | |
const hideImage = useCallback((imageUrl) => { | |
setData((prevData) => { | |
if (prevData.includes(imageUrl)) { | |
return prevData.filter(image => image !== imageUrl); | |
} | |
return prevData; | |
}); |
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
import is from './objectIs'; | |
import hasOwnProperty from './hasOwnProperty'; | |
/** | |
* Performs equality by iterating through keys on an object and returning false | |
* when any key has values which are not strictly equal between the arguments. | |
* Returns true when the values of all keys are strictly equal. | |
*/ | |
function shallowEqual(objA: mixed, objB: mixed): boolean { | |
if (is(objA, objB)) { | |
return true; |
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
function shallowEqual(objA: mixed, objB: mixed): boolean { | |
// ... | |
} |
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
import is from './objectIs'; | |
function shallowEqual(objA: mixed, objB: mixed): boolean { | |
if (is(objA, objB)) { | |
return true; | |
} | |
// ... | |
} |
OlderNewer