小编And*_*dru的帖子

React hooks:为什么异步函数中的多个 useState setter 会导致多次重新渲染?

以下 onClick 回调函数将导致 1 次重新渲染:

const handleClickSync = () => {
  // Order of setters doesn't matter - React lumps all state changes together
  // The result is one single re-rendering
  setValue("two");
  setIsCondition(true);
  setNumber(2);
};
Run Code Online (Sandbox Code Playgroud)

React 将所有三个状态更改集中在一起并导致 1 次重新渲染。

然而,以下 onClick 回调函数将导致 3 次重新渲染:

const handleClickAsync = () => {
  setTimeout(() => {
    // Inside of an async function (here: setTimeout) the order of setter functions matters.
    setValue("two");
    setIsCondition(true);
    setNumber(2);
  });
};
Run Code Online (Sandbox Code Playgroud)

这对每个设置者来说都是一次重新渲染useState。此外,设置器的顺序会影响每个渲染中的值。

问题:为什么我使函数异步(此处为 via setTimeout)这一事实会导致状态更改相继发生,从而导致 …

javascript reactjs react-hooks

31
推荐指数
2
解决办法
5469
查看次数

npm install卡在fetchMetadata上

npm install从今天起,我目前无法参与任何项目.我正在运行节点v8.2.1和npm 5.3.0(通过nvm安装).

键入npm installfetchMetadata每次都会卡住: ? ????????????????? ? fetchMetadata: sill pacote range manifest for longest@^1.0.1 fetched in 197ms

我已经尝试切换回npm 5.0.3哪个功能完美无缺,但仍然卡住了.

我的电脑详细信息: MacBook Pro正在运行macOS 10.12.6

node.js npm

15
推荐指数
7
解决办法
2万
查看次数

具有动态键的对象的Apollo/GraphQL字段类型

假设我的graphql服务器想要获取以下数据作为JSON,其中person3person5是一些id:

"persons": {
  "person3": {
    "id": "person3",
    "name": "Mike"
  },
  "person5": {
    "id": "person5",
    "name": "Lisa"
  }
}
Run Code Online (Sandbox Code Playgroud)

问题:如何使用apollo创建模式类型定义?

密钥person3person5这里是动态生成的,具体取决于我的查询(即area在查询中使用).因此,在其他时间我可能会person1,person2,person3返回.如您所见persons,不是Iterable,因此以下不能用作我用apollo做的graphql类型定义:

type Person {
  id: String
  name: String
}
type Query {
  persons(area: String): [Person]
}
Run Code Online (Sandbox Code Playgroud)

persons对象中的键可能总是不同的.

当然,一种解决方案是将传入的JSON数据转换为使用数组persons,但是没有办法处理数据吗?

apollo graphql apollo-server

13
推荐指数
1
解决办法
6609
查看次数

Jest:在测试套件中更改手动模拟的输出以用于不同的测试

假设我有以下两个文件:

// index.js
...
import { IS_IOS } from 'common/constants/platform';
...
export const myFunction = () => (IS_IOS ? 'foo' : 'bar');


// index.test.js
...
import { myFunction } from './index';

jest.mock('common/constants/platform', () => ({ IS_IOS: true }));

describe('My test', () => {
  it('tests behavior on IOS', () => {
    expect(myFunction()).toBe('foo');
  });

  // --> Here I want to change the value of IS_IOS to false

  it('tests behavior if NOT IOS', () => {
    expect(myFunction()).toBe('bar');
  });
});
Run Code Online (Sandbox Code Playgroud)

如你所见,我的模拟函数返回IS_IOS: true.我希望它 …

javascript unit-testing mocking jestjs

8
推荐指数
1
解决办法
828
查看次数

宅基地初始没有定义

在按照4.2版的官方安装说明安装homestead时,我在执行时收到以下错误消息$ homestead init: 错误信息

 [Symfony\Component\Console\Exception\CommandNotFoundException]  
  Command "init" is not defined.
Run Code Online (Sandbox Code Playgroud)

我之前的步骤是安装宅基地 composer global require "laravel/homestead=~3.0"

我安装Laravel Homestead version 3.0.1homestead在我的PATH.

php laravel homestead

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

React native Android SyntaxError:尝试重新定义属性'key'

运行后,我在仿真器(测试的Genymotion和谷歌股票仿真器)和物理设备(三星S4)上收到以下错误react-native run-android:

在此输入图像描述

错误输入: java.util.concurrent.ExcecutionException: java.lang.RuntimeException: SyntaxError: Attempted to redefine property 'key'. (http://192.168.50.37:8081/index.android.bundle?platform=android&dev=true&hot=false:72450)

  • 这个stackoverflow讨论中java.util.concurrent.ExecutionException,Genymotion发生了类似的错误(也是一个),但那里的解决方案没有帮助我,即adb reverse tcp:8081 tcp:8081.
  • 我也试图改变开发设置/ Debug server host & port for device0.0.0.0:8081,而不是MY_IP:8081,这里MY_IP是我的电脑的IP.

我正在运行反应原生0.22.2

java android adb genymotion react-native

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

为什么重载函数声明有时会强制无用的类型缩小?

给出重载函数语句的以下实现:

\n
function foo(options: "a"): "a";\nfunction foo(options: "b"): "b";\nfunction foo(options: "a" | "b"): "a" | "b" {\n    switch (options) {\n        case "a":\n            return `a`;\n        case "b":\n        default:\n            return `b`;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我可以这样调用该函数:

\n
// this works\nconst url1 = foo("a");\nconst url2 = foo("b");\n
Run Code Online (Sandbox Code Playgroud)\n

foo但是,如果在使用联合类型的值调用该函数时调用该函数,则会出现类型错误"a" | "b"

\n
// function call (inside of wrapped function)\ntype Options = "a" | "b";\nconst wrapper = (options: Options) => {\n    // function overloading forces the caller to narrow the type\n    const url …
Run Code Online (Sandbox Code Playgroud)

typescript

3
推荐指数
1
解决办法
962
查看次数

如何将异常转换为NSError对象

我想将异常消息转换为NSError对象,以便可以在try-catch块中使用它(我实际上是在React Native本机iOS模块上工作)。

RCT_EXPORT_METHOD(myMethod:(NSDictionary *)dict
             resolver:(RCTPromiseResolveBlock)resolve
             rejecter:(RCTPromiseRejectBlock)reject)
{
  @try {
    // Do something which could throw something (NS Error or NS Exception)
    resolve(nil);
  } @catch (NSException *exception) {
    // HERE I WANT TO TRANSFORM THE EXCEPTION exception INTO AN ERROR error
    NSError error = ???
    reject(@"my_error", @"Could not do something important", error);
  }
}
Run Code Online (Sandbox Code Playgroud)

我想将异常转换为,NSError因为reject函数的第三个参数(拒绝JS端的Promise)期望输入的类型为NSError。我不确定我的解决方案(使用try-catch)在这种情况下是否是最好的方法。

此《 Apple开发人员指南》中,它说

您可以将异常转换为NSError对象,然后在警报面板中将错误对象中的信息呈现给用户。

但是,该指南并未显示该示例的代码示例,而仅显示了第二种方法的代码示例。您也可以在包含错误参数的方法中间接返回它们,这些错误参数对于我想要的而言似乎很复杂。

那么,如何将异常转换为NSError?NSError的API参考似乎不包含合适的功能。

exception-handling objective-c react-native-ios

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

您如何根据数据框中的某些数字计算总和?

我有这样的变量

a = pd.DataFrame(np.array([[1, 1, 2, 3, 2], [2, 2, 3, 3, 2], [1, 2, 3, 2, 3]]))

b = np.array([0.1, 0.3, 0.5, 0.6, 0.2])
Run Code Online (Sandbox Code Playgroud)

展示 a

   0    1      2      3      4                                         
0  1    1      2      3      2                    
1  2    2      3      3      2
2  1    2      3      2      3
Run Code Online (Sandbox Code Playgroud)

展示 b

[0.1    0.3    0.5    0.6    0.2]
Run Code Online (Sandbox Code Playgroud)

我想要的结果是b基于a其中的索引a作为 中值的索引的值的总和b

我想要的最终结果是这样的。

 0.4   0.7  0.6
   0   0.5  0.11                     
 0.1   0.9  0.7
Run Code Online (Sandbox Code Playgroud)

如何详细获取第一行 …

python numpy pandas

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

为什么 TypeScript `never` 类型需要问号?对于条件对象

这段代码可以很好地有条件地要求该isDirty字段成为讲座对象的一部分:

  • 如果id是 type string,我必须添加一个isDirty字段:
  • 如果id是 type number,则无法isDirty向对象添加字段。
type LectureT = LectureIdT & {
  title: string;
};

/**
 * Lecture must have an `isDirty` field if `id` is of type string, but should have no `isDirty` field if `id` if of type number.
 */
type LectureIdT =
  | { id: string; isDirty: boolean }
  | { id: number; isDirty?: never };
  

const lectureMockWithNumberId: LectureT = {
  id: 1,
  title: …
Run Code Online (Sandbox Code Playgroud)

javascript typescript

0
推荐指数
1
解决办法
55
查看次数