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
// useBoolean.js | |
import { useState, useCallback } from "react" | |
const useBoolean = (initialState) => { | |
const [booleanValue, setBooleanValue] = useState(initialState) | |
const setValueToTrue = useCallback(() => setBooleanValue(true), [setBooleanValue]) | |
const setValueToFalse = useCallback(() => setBooleanValue(false), [setBooleanValue]) | |
const toggleValue = useCallback(() => setBooleanValue(!booleanValue), [booleanValue, setBooleanValue]) | |
return [booleanValue, setValueToTrue, setValueToFalse, toggleValue] |
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 React, {useState} from "react" | |
import { ComponentOne, ComponentTwo } from "./somewhere" | |
const ExampleComponent = (props) => { | |
const [selected, setSelected] = setState(false) | |
const setSelectedTrue = () => setSelected(true) | |
const setSelectedFalse = () => setSelected(false) | |
const toggleSelected = () => setSelected(!selected) |
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
// CountingButton.js | |
import React from "react" | |
const CountingButton = ({ onClick, count }) => ( | |
<button onClick={onClick}> | |
{`You clicked me ${count} times!`} | |
</button> | |
) | |
export default CountingButton |
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
// CountingButton.js | |
import React, { useState, useEffect } from "react" | |
const CountingButton = ({ onClick, count }) => { | |
const [displayText, setDisplayText] = useState("") | |
useEffect(() => { | |
setDisplayText(`You clicked me ${count} times!`) | |
}, [count, setDisplayText]) |