美文网首页
useReducer

useReducer

作者: Adder | 来源:发表于2021-03-16 17:30 被阅读0次

    1.如何使用useReducer

    const reducer =(state = 0, {type})=>{
        switch (type) {
            case "add":
                return state+1
            case 'delete':
                return state-1
            default:
                return state;
        }
    }
    
    const Hook =()=>{
        const [count, dispatch] = useReducer(reducer, 0)
        return(
            <div>
               count:{count}
               <button onClick={()=> dispatch({type:'add'})}>add</button>
                <button onClick={()=> dispatch({type:'delete'})}>delete</button>
            </div>
        )
    }
    
    export default Hook
    
    

    相关文章

      网友评论

          本文标题:useReducer

          本文链接:https://www.haomeiwen.com/subject/juidcltx.html