zustand 选择器何时取决于范围?

问题描述

zutand doc 提到如果选择器不依赖于范围,您可以使用选择器并提供下面的示例

enter image description here

我不明白 cmake_minimum_required(VERSION 3.4.1) add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp ) find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib}) 部分。有人可以提供一些示例,说明何时状态取决于范围,因此不应使用选择器?

谢谢!

解决方法

这意味着选择器是一个仅使用 state 对象的函数,因此没有外部依赖项。

有人可以提供一些示例,说明状态何时取决于 范围以便不应使用选择器?

作用域不会影响何时何地可以使用选择器函数,而是可以在何处声明它。除了 state 之外,没有依赖项的选择器函数可以在使用它们的任何组件的外部声明。

您可以编写一个选择器函数,该函数还引用组件中定义的值,因此该值将是外部依赖项。

const MyComponent = () => {
  const value = ......;

  const selector = state = {
    // use enclosed value to compute returned value from state
    ... do something with value
  };

  const valueFromState = useStore(selector);

  ...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...