Reputation: 47
Simple homework problem working on React HOC.
I thought it was simple... HOC [withClapAnimation] passes animate
function to [MediumClap]. animate
merely console.logs. [MediumClap] is button that should spit out the animate
when triggered... I get the response animate is not a function
Any ideas?
before animate
import React, {Component, useState} from 'react'
import styles from './index.css'
const initialState = {
count: 0,
countTotal: 200,
isClicked: false
}
const withClapAnimation = WrappedComponent => {
class WithClapAnimation extends Component {
render () {
return <WrappedComponent {...this.props} />
}
}
return WrappedComponent
}
const MediumClap = () => {
const MAX_USER_CLAP = 10
const [clapState, setClapState] = useState(initialState)
const {count, countTotal, isClicked } = clapState
const handleClapClick = () => {
setClapState(prevState => ({
isClicked: true,
count: Math.min(count + 1, MAX_USER_CLAP),
countTotal:
count < MAX_USER_CLAP
? prevState.countTotal + 1
: prevState.countTotal
}))
}
return <button className={styles.clap} onClick={handleClapClick}>
<ClapIcon isClicked={isClicked} />
<ClapCount count={count} />
<ClapTotal countTotal={countTotal} />
</button>
}
const ClapIcon = ( {isClicked}) => {
return <span>
<svg
xmlns="http://www.w3.org/2000/svg"
fill-rule="evenodd"
stroke-linejoin="round"
stroke-miterlimit="2"
clip-rule="evenodd"
viewBox="0 0 100 125"
className={`${styles.icon} ${isClicked && styles.checked}`}
>
<defs/>
<g fill-rule="nonzero">
<path d="M45.5 50.7c-2.1-.4-4.7.7-5.7 4.8-.5 2.2-.4 5.8-.2 9.6v.7c0 .9-.7 1.6-1.6 1.5-.7-.1-1.2-.7-1.2-1.4v-.6c-.1-3-.2-5.6-.1-7.8.1-.9.2-1.8.4-2.5.7-3.2 2.4-5.2 4.3-6.2.9-3.1 1.7-6 2.5-7.7 2.1-5-4.1-8.6-7.5-4.2-3.5 4.3-14.1 19.8-14 28.1 0 3.8.9 9.7 2.4 15.2.2.9 0 1.8-.6 2.5l-2.4 2.9c-.7.9-.6 2.2.2 2.9L32.2 97c.9.7 2.2.6 2.9-.3 1.4-1.8 3.8-4.7 5.7-7.4 4-5.4 6.6-11.6 7.1-14.7.4-2.8.5-6.7.4-10.6-.1-3.3-.2-6.8.2-9.7.3-2-1.3-3.3-3-3.6zM78.2 85.5l-2.4-2.9c-.6-.7-.8-1.6-.6-2.5 1.5-5.5 2.4-11.4 2.4-15.2.1-8.3-10.5-23.8-13.9-28.3-3.4-4.4-9.6-.8-7.5 4.2.7 1.7 1.6 4.6 2.5 7.7 1.9 1 3.5 3 4.3 6.2.2.8.3 1.6.4 2.5.2 2.2.1 4.9-.1 7.8v.6c0 .7-.5 1.4-1.2 1.4-.9.1-1.7-.6-1.6-1.5v-.7c.2-3.8.3-7.4-.2-9.6-1-4.1-3.6-5.2-5.7-4.8-1.7.3-3.3 1.6-3.1 3.7.3 2.9.3 6.4.2 9.7-.1 3.8 0 7.8.4 10.6.5 3.1 3.1 9.3 7.1 14.7 2 2.6 4.3 5.6 5.7 7.4.7.9 2 1 2.9.3L78 88.4c.8-.7.9-2 .2-2.9zM53.7 18V6.1c0-2-1.6-3.6-3.6-3.6s-3.6 1.6-3.6 3.6V18c0 2 1.6 3.6 3.6 3.6 1.9.1 3.6-1.6 3.6-3.6zM33.7 26.3c.7.7 1.6 1.1 2.6 1.1.9 0 1.9-.4 2.6-1.1 1.4-1.4 1.4-3.7 0-5.1l-8.4-8.4c-1.4-1.4-3.7-1.4-5.1 0-1.4 1.4-1.4 3.7 0 5.1l8.3 8.4zM63.8 27.4c.9 0 1.9-.4 2.6-1.1l8.4-8.4c1.4-1.4 1.4-3.7 0-5.1-1.4-1.4-3.7-1.4-5.1 0l-8.4 8.4c-1.4 1.4-1.4 3.7 0 5.1.6.7 1.6 1.1 2.5 1.1z "/>
</g>
</svg>
</span>
}
const ClapCount = ({count}) => {
return <span className={styles.count}> + {count} </span>
}
const ClapTotal = ({countTotal}) => {
return <span className={styles.total}> {countTotal} </span>
}
const Usage = () => {
const AnimatedMediumClap = withClapAnimation(MediumClap)
return <AnimatedMediumClap />
}
export default Usage
after adding animate
import React, {Component, useState} from 'react'
import styles from './index.css'
const initialState = {
count: 0,
countTotal: 200,
isClicked: false
}
const withClapAnimation = WrappedComponent => {
class WithClapAnimation extends Component {
animate = () => {
console.log('animate')
}
render () {
return <WrappedComponent {...this.props} animate={this.animate} />
}
}
return WrappedComponent
}
const MediumClap = ({animate}) => {
const MAX_USER_CLAP = 10
const [clapState, setClapState] = useState(initialState)
const {count, countTotal, isClicked } = clapState
const handleClapClick = () => {
animate()
setClapState(prevState => ({
isClicked: true,
count: Math.min(count + 1, MAX_USER_CLAP),
countTotal:
count < MAX_USER_CLAP
? prevState.countTotal + 1
: prevState.countTotal
}))
}
return <button className={styles.clap} onClick={handleClapClick}>
<ClapIcon isClicked={isClicked} />
<ClapCount count={count} />
<ClapTotal countTotal={countTotal} />
</button>
}
const ClapIcon = ( {isClicked}) => {
return <span>
<svg
xmlns="http://www.w3.org/2000/svg"
fill-rule="evenodd"
stroke-linejoin="round"
stroke-miterlimit="2"
clip-rule="evenodd"
viewBox="0 0 100 125"
className={`${styles.icon} ${isClicked && styles.checked}`}
>
<defs/>
<g fill-rule="nonzero">
<path d="M45.5 50.7c-2.1-.4-4.7.7-5.7 4.8-.5 2.2-.4 5.8-.2 9.6v.7c0 .9-.7 1.6-1.6 1.5-.7-.1-1.2-.7-1.2-1.4v-.6c-.1-3-.2-5.6-.1-7.8.1-.9.2-1.8.4-2.5.7-3.2 2.4-5.2 4.3-6.2.9-3.1 1.7-6 2.5-7.7 2.1-5-4.1-8.6-7.5-4.2-3.5 4.3-14.1 19.8-14 28.1 0 3.8.9 9.7 2.4 15.2.2.9 0 1.8-.6 2.5l-2.4 2.9c-.7.9-.6 2.2.2 2.9L32.2 97c.9.7 2.2.6 2.9-.3 1.4-1.8 3.8-4.7 5.7-7.4 4-5.4 6.6-11.6 7.1-14.7.4-2.8.5-6.7.4-10.6-.1-3.3-.2-6.8.2-9.7.3-2-1.3-3.3-3-3.6zM78.2 85.5l-2.4-2.9c-.6-.7-.8-1.6-.6-2.5 1.5-5.5 2.4-11.4 2.4-15.2.1-8.3-10.5-23.8-13.9-28.3-3.4-4.4-9.6-.8-7.5 4.2.7 1.7 1.6 4.6 2.5 7.7 1.9 1 3.5 3 4.3 6.2.2.8.3 1.6.4 2.5.2 2.2.1 4.9-.1 7.8v.6c0 .7-.5 1.4-1.2 1.4-.9.1-1.7-.6-1.6-1.5v-.7c.2-3.8.3-7.4-.2-9.6-1-4.1-3.6-5.2-5.7-4.8-1.7.3-3.3 1.6-3.1 3.7.3 2.9.3 6.4.2 9.7-.1 3.8 0 7.8.4 10.6.5 3.1 3.1 9.3 7.1 14.7 2 2.6 4.3 5.6 5.7 7.4.7.9 2 1 2.9.3L78 88.4c.8-.7.9-2 .2-2.9zM53.7 18V6.1c0-2-1.6-3.6-3.6-3.6s-3.6 1.6-3.6 3.6V18c0 2 1.6 3.6 3.6 3.6 1.9.1 3.6-1.6 3.6-3.6zM33.7 26.3c.7.7 1.6 1.1 2.6 1.1.9 0 1.9-.4 2.6-1.1 1.4-1.4 1.4-3.7 0-5.1l-8.4-8.4c-1.4-1.4-3.7-1.4-5.1 0-1.4 1.4-1.4 3.7 0 5.1l8.3 8.4zM63.8 27.4c.9 0 1.9-.4 2.6-1.1l8.4-8.4c1.4-1.4 1.4-3.7 0-5.1-1.4-1.4-3.7-1.4-5.1 0l-8.4 8.4c-1.4 1.4-1.4 3.7 0 5.1.6.7 1.6 1.1 2.5 1.1z "/>
</g>
</svg>
</span>
}
const ClapCount = ({count}) => {
return <span className={styles.count}> + {count} </span>
}
const ClapTotal = ({countTotal}) => {
return <span className={styles.total}> {countTotal} </span>
}
const Usage = () => {
const AnimatedMediumClap = withClapAnimation(MediumClap)
return <AnimatedMediumClap />
}
export default Usage
Upvotes: 0
Views: 239
Reputation: 3507
in withClapAnimation HOC your getting the component WrappedComponent
and returning it without applying any changes instead you need to return the modified component WithClapAnimation
like this:
const withClapAnimation = WrappedComponent => {
class WithClapAnimation extends Component {
animate = () => {
console.log('animate')
}
render () {
return <WrappedComponent {...this.props} animate={this.animate} />
}
}
return WithClapAnimation
}
Upvotes: 2