`createContext()和`createSignal()`对于全局状态
在SOLID-JS中,使用上下文 a href =“ https://www.solidjs.com/docs/latest#createsignal” rel =“ noreferrer”>信号并导入它是getter/setter/nes getter/setter/甚至是从商店类似的全球商店式文件中的回忆价值/ ?:
// ./store/myValue.js
export const [value, setValue] = createSignal('');
据我了解,在多个组件中导入此value
完全可以,并且在整个过程中都可以保持反应性。
我能想到的商店/全局的唯一用例是,如果您的状态变得如此复杂,则您需要一种更结构化的上下文方法来提高可维护性,可读性等。
In solid-js, what is the benefit of using context, when we can create a signal and import it's getter/setter/even a memoized value from a store global store-like file?:
// ./store/myValue.js
export const [value, setValue] = createSignal('');
To my understanding it is totally ok to import this value
in multiple components and the reactivity would be maintained throughout those.
The only use-case of the store/global that I can think of, is if your state grows to such a complexity, that you need a more structured context-like approach to improve maintainability, readability etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
固体中的上下文是包裹其子范围的范围对象。上下文的主要好处是,您可以在组件树的不同级别上重写上下文值。换句话说,您可以同时在不同的上下文中具有不同的值。
在下面查看此示例。当您深入到组件树时,我们可以切换主题。全球商店将在每个级别为您提供相同的价值。
这将输出:
Context in Solid is a scoped object that wraps its child scopes. The main benefit of context is you can re-write a context value at different levels of the component tree. It other words, you can have different values in different contexts, all at the same time.
Check this example below. We can switch theme as you go deeper into the component tree. A global store will give you the same value at every level.
This will output:
上下文的好处是能够将状态绑定到组件树。想象一下,您有一个需要重复使用的单选按钮组。全球状态意味着您只能在页面中使用一次,以免下一个用法覆盖前一个用法 - 将状态与组件联系到上下文将使此任务变得更加容易。
The benefit of context is the ability to bind a state to your component tree. Imagine you have a radio button group that you want to reuse. A global state would mean you could only ever use it once in your page, lest the next usage will overwrite the previous one - tying the state to the component with a context will make this task a lot easier.