小编Avr*_*zky的帖子

reduceReducers()如何工作?

代码来自reduce-reducer repo:

   export function reduceReducers(...reducers) {
        return (previous, current) =>
            reducers.reduce(
                (p, r) => r(p, current),
                previous
            );
    }
Run Code Online (Sandbox Code Playgroud)

我知道这个函数的目的是在Redux中展平不同的状态切片,请参见此处,但我不明白这个函数是如何工作的.我查了MDN,但还是不明白.

什么是呼唤previous,current做什么pr代表什么.我无法识别被调用的变量.

编辑

Mark Erikson在他的Practical Redux系列中定义了这个功能:

reduceReducers是一个漂亮的小实用程序.它允许我们提供多个reducer函数作为参数,并有效地从这些函数中形成一个管道,然后返回一个新的reducer函数.如果我们将这个新的reducer称为顶级状态,它将调用带有状态的第一个输入reducer,将其输出传递给第二个输入reducer,依此类推.

编辑2

我写了一篇文章来解释reduceReducers().

javascript redux

7
推荐指数
1
解决办法
921
查看次数

Django 是否缓存数据库的 DNS 信息?

Django 在连接到 postgres 时似乎没有进行 DNS 查找

我有一个 TTL 为 1 秒的 Route53 加权 CNAME 记录(只是为了测试这一点)来平衡我们的读取代表的负载,但是当我调整权重并使用 dig 对其进行测试时,确保只有两个读取副本 ip 之一正在被使用由 DNS 返回,Django 继续使用 DNS 交换之前的数据库 IP。

Django 是否缓存其数据库的 IP 以避免 DNS 查找?我希望它每次以 conn_max_age 为 0 连接时都会查找数据库。

django dns caching django-orm

6
推荐指数
0
解决办法
549
查看次数

获取 React-Select 中突出显示选项的值

我正在使用带有异步选择组件的react-select v2

        <AsyncSelect
            cacheOptions
            components={{ Option }}
            loadOptions={this.getSearchItems}
            onInputChange={this.handleInputChange}
            placeholder="Search..."
            onKeyDown={this._handleKeyPress}
        />
Run Code Online (Sandbox Code Playgroud)

如何访问因使用键盘上的向下键或鼠标悬停而突出显示的选项的值?

我想根据突出显示的选项触发重定向或 setState 。onKeyDown仅将输入值作为发送event.target.value

这是 Nordstrom 网站上的一个例子:

在此处输入图片说明

reactjs react-select

5
推荐指数
1
解决办法
8609
查看次数

Solr - 限制更像这个结果

比方说,我有一个特别的毛衣code:blue-sweatercolor:blue.我想找到使用该description领域的类似产品,其约束条件是类似的产品不是blue(-color:blue).

来自Solr维基:

如果你想过滤MoreLikeThis给出的类似结果,你必须使用MoreLikeThisHandler.它会将类似的文档结果集视为主要结果集,因此将对其应用指定的过滤器(fq).如果使用MoreLikeThisComponent并应用查询过滤器,它将应用于主查询(QueryComponent)返回的结果集,而不是应用于MoreLikeThisComponent返回的结果集.

这些是我正在使用的参数; 所述qtPARAM设置请求处理程序为MoreLikeThis:

    {
      q: "code:"blue-sweater"",
      qt: "mlt",
      mlt: "true",
      fl: "description,brand,gender,price",
      mlt.boost: "true",
      mlt.fl: "description",
      fq: "-color:"blue"",
      rows: "6",
      mlt.mintf: "0",
      mlt.mindf: "0"
    }
Run Code Online (Sandbox Code Playgroud)

问题是我只能指定FilterQuery一次参数,它fq为初始查询("code:"blue-sweater")和MoreLikeThis结果设置.

由于过滤器-color:blue排除了我的初始查询(蓝色毛衣),我没有更多喜欢这个结果.我该如何解决这个问题?

如果核心中的唯一产品是color:blue,我仍然想要归还它们,但它们应该是可能结果的底部.

编辑

我做了一些挖掘,似乎提升MoreLikeThis查询的唯一方法是mlt.qf:

使用与DisMax Query Parser使用的格式相同的格式查询字段及其提升.这些字段也必须在mlt.fl中指定.(来源)

我试图使用具有值约束(如in_stock:[* TO 10])的DisMax解析器进行常规查询,但是对字段值的约束将被完全忽略.你只能对一个字段(color^2)进行简单的提升.

因此,似乎这是对MoreLike的限制.这依赖于DisMax解析器而不是EdisMax解析器.

lucene solr

5
推荐指数
0
解决办法
96
查看次数

Postgres Materialized Path - What are the benefits of using ltree?

Materialized Path is a method for representing hierarchy in SQL. Each node contains the path itself and all its ancestors (grandparent/parent/self).

The django-treebeard implementation of MP (docs):

  1. Each step of the path is a fixed length for consistent performance.

  2. Each node contains depth and numchild fields (fast reads at minimal cost to writes).

  3. The path field is indexed (with a standard b-tree index):

    The materialized path approach makes heavy use of LIKE in your database, with …

postgresql hierarchy ltree

5
推荐指数
1
解决办法
1700
查看次数

React Select with Async - 无法加载选项

我正在尝试创建一个react-select异步输入字段,该字段根据用户输入提供型号。我的 API 和 Fetch 都在工作,它正在生成填充下拉列表所需的确切“选项”对象(我检查了网络请求和控制台)。

问题是当我开始输入时,下拉列表显示它正在加载,但没有添加任何选项。

在此处输入图片说明

以下是 Fetch 调用的示例输出: {options: [{value: "WA501006", label: "WA501006"}]}.

这是我的代码:

getModelsAPI = (input) => {

        if (!input) {
            return Promise.resolve({ options: [] });
        }

        const url = `(...)?cmd=item_codes_json&term=${input}`;

        fetch(url, {credentials: 'include'})
            .then((response) => {
                return response.json();
            })
            .then((json) => {
                const formatted = json.map((l) => {
                    return Object.assign({}, {
                        value: l.value,
                        label: l.label
                    });
                })
                return { options: formatted }
            })
    }

onChange = (value) => {
    this.setState({
        value: value
    })
}

<Select.Async …
Run Code Online (Sandbox Code Playgroud)

reactjs react-select

4
推荐指数
1
解决办法
4943
查看次数

带有 Promise 的流类型(Fetch)

我创建了一个 Fetch 函数来使用 JSON API,并为 JSON 对象定义了类型。我对如何定义函数的返回类型感到困惑, getCurrentJobAPI因为我后来做了很多工作.then()。返回值是最后一个.then()吗?在我的代码中,最后一个 .then() 是一个 setState,那么它的类型是什么?

    getCurrentJobAPI = (): {} => {

        const url: string = `dummy_url&job_id=${this.props.currentJob}`;

        return fetch(url, {credentials: 'include'})
            .then((response) => {
                return response.json();
            })
            .then((json: CurrentJob) => {
                console.log(json);
                const location = json.inventoryJob.location;
                const ref_note = json.inventoryJob.note;
                const id = json.inventoryJob.id;
                const models = json.inventoryJobDetails.map((j) => {
                    return Object.assign({}, {
                        code: j.code,
                        qty: j.qty
                    })
                });
                this.setState({ currentCodes: models, location: location, ref_note: ref_note, id: id})
                return json
            })
            .then((json: …
Run Code Online (Sandbox Code Playgroud)

javascript flowtype

4
推荐指数
1
解决办法
3844
查看次数

动态导入 - NextJS

我有一个加载脚本的简单函数:

const creditCardScript = (
  onReadyCB,
  onErrorCB,
) => {
  let script = document.createElement("script");
  script.type = "text/javascript";
  script.src = process.CREDIT_CARD_SCRIPT;
  document.head.appendChild(script);

  script.onload = function() {
    ...
  };
};

export default creditCardScript;
Run Code Online (Sandbox Code Playgroud)

在迁移到 NextJS 之前,我使用以下命令导入脚本import creditCardScript from "./creditCardScript"

Sine NextJS 在 Node 中的服务器端渲染组件,需要注意确保任何引用window(特定于浏览器)的代码在 之前不会被调用componentDidMount

NextJS 通过提供动态导入( react-loadable 的包装器)来解决这个问题,其中:

  • 仅在需要时加载组件,
  • 提供仅在客户端加载组件的选项 ( ssr: false)。

我继续实施动态导入:

const creditCardScript = dynamic(import("./creditCardScript"), { ssr: false });
Run Code Online (Sandbox Code Playgroud)

componentDidMount

componentDidMount = () => {
  creditCardScript(
    this.onReadyCB, …
Run Code Online (Sandbox Code Playgroud)

javascript node.js reactjs next.js react-loadable

4
推荐指数
1
解决办法
2万
查看次数