Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增useSharedState #2009

Closed
wants to merge 3 commits into from
Closed

Conversation

IVLIU
Copy link

@IVLIU IVLIU commented Dec 16, 2022

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

💡 Background and solution

  1. Provide a global state solution other than Context
  2. Only re-render when subscription value changes
  3. As easy as useState to use

useage

import { createSharedState } from 'ahooks';

// 创建一个全局状态,可以是任何类型的值
const useCount = createSharedState({ value1: 0, value2: 0 });

function Counter() {
  const [count, setCount] = useCount();
  return (
    <>
      <p onClick={() => setCount({
       value1: count.value1 + 1,
       value2: count.value2
     })}>{count.value1}</p>
     <p onClick={() => setCount({
       value1: count.value1,
       value2: count.value2 + 1
     })}>{count.value2}</p>
    </>
 )
}
function Counter2() {
  // 如果只更新value1,那组件不会重新渲染
  const count2 = useCount((state) => state.value2)[0];
  return <p>{count2}</p>
}

📝 Changelog

Language Changelog
🇺🇸 English add a new hook createSharedState
🇨🇳 Chinese 新增一个hook createSharedState

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


liuchuan seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@miracles1919
Copy link
Collaborator

社区已经有很多成熟的状态管理库了,ahooks 应该不会再造轮子了

@crazylxr
Copy link
Collaborator

crazylxr commented Jan 3, 2023

你的这个 跟这个 issue #1832 是一样的,暂时不会考虑这个哈

@crazylxr crazylxr closed this Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants