Skip to content

Instantly share code, notes, and snippets.

View Keraito's full-sized avatar

Chak Shun Yu Keraito

  • Rotterdam, Netherlands
View GitHub Profile
import is from './objectIs';
function shallowEqual(objA: mixed, objB: mixed): boolean {
if (is(objA, objB)) {
return true;
}
// ...
}
function shallowEqual(objA: mixed, objB: mixed): boolean {
// ...
}
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;
const Component = () => {
const [images, setImages] = useState([]);
const hideImage = useCallback((imageUrl) => {
setData((prevData) => {
if (prevData.includes(imageUrl)) {
return prevData.filter(image => image !== imageUrl);
}
return prevData;
});
const Component = () => {
const [showImage, setShowImage] = useState(true);
const hideImage = useCallback(() => {
if (showImage) {
setShowImage(false);
}
}, [showImage]);
return (
const useIdsHandler = () => {
const [ids] = useArrayIds();
const [storedIds, setStoredIds] = useState(ids);
useEffect(() => {
setStoredIds(ids);
}, [ids]);
const removeId = useCallback((idToRemove) => {
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);
}, []);
const Component = () => {
const [ids] = useArrayIds();
const [storedIds, setStoredIds] = useState(ids);
const removeId = useCallback((idToRemove) => {
setStoredIds((oldIds) => oldIds.filter((oldId) => oldId !== idToRemove));
}, []);
useEffect(() => {
setStoredIds(ids);
// 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.
});
function mapDispatchToProps(dispatch) {
return {
doSomething: () => {
dispatch(somethingDispatchable());
},
};
}
class SomeComponent extends React.Component {
componentDidMount() {