useState和useImmer是React中的两个钩子函数,用于管理组件的状态。
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increase</button>
</div>
);
}
import React from 'react';
import { useImmer } from 'use-immer';
function Example() {
const [state, setState] = useImmer({ count: 0 });
return (
<div>
<p>Count: {state.count}</p>
<button onClick={() => setState(draft => { draft.count += 1; })}>Increase</button>
</div>
);
}
总结:
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云