以下 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
)这一事实会导致状态更改相继发生,从而导致 …
npm install
从今天起,我目前无法参与任何项目.我正在运行节点v8.2.1和npm 5.3.0(通过nvm安装).
键入npm install
时fetchMetadata
每次都会卡住:
? ????????????????? ? fetchMetadata: sill pacote range manifest for longest@^1.0.1 fetched in 197ms
我已经尝试切换回npm 5.0.3
哪个功能完美无缺,但仍然卡住了.
我的电脑详细信息: MacBook Pro正在运行macOS 10.12.6
假设我的graphql服务器想要获取以下数据作为JSON,其中person3
和person5
是一些id:
"persons": {
"person3": {
"id": "person3",
"name": "Mike"
},
"person5": {
"id": "person5",
"name": "Lisa"
}
}
Run Code Online (Sandbox Code Playgroud)
问题:如何使用apollo创建模式类型定义?
密钥person3
和person5
这里是动态生成的,具体取决于我的查询(即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
,但是没有办法处理数据吗?
假设我有以下两个文件:
// 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
.我希望它 …
在按照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.1
并homestead
在我的PATH
.
运行后,我在仿真器(测试的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)
java.util.concurrent.ExecutionException
,Genymotion发生了类似的错误(也是一个),但那里的解决方案没有帮助我,即adb reverse tcp:8081 tcp:8081
.Debug server host & port for device
对0.0.0.0:8081
,而不是MY_IP:8081
,这里MY_IP
是我的电脑的IP.我正在运行反应原生0.22.2
给出重载函数语句的以下实现:
\nfunction 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)\nfoo
但是,如果在使用联合类型的值调用该函数时调用该函数,则会出现类型错误"a" | "b"
:
// 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) 我想将异常消息转换为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)在这种情况下是否是最好的方法。
您可以将异常转换为NSError对象,然后在警报面板中将错误对象中的信息呈现给用户。
但是,该指南并未显示该示例的代码示例,而仅显示了第二种方法的代码示例。您也可以在包含错误参数的方法中间接返回它们,这些错误参数对于我想要的而言似乎很复杂。
那么,如何将异常转换为NSError?NSError的API参考似乎不包含合适的功能。
我有这样的变量
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)
如何详细获取第一行 …
这段代码可以很好地有条件地要求该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 ×3
typescript ×2
adb ×1
android ×1
apollo ×1
genymotion ×1
graphql ×1
homestead ×1
java ×1
jestjs ×1
laravel ×1
mocking ×1
node.js ×1
npm ×1
numpy ×1
objective-c ×1
pandas ×1
php ×1
python ×1
react-hooks ×1
react-native ×1
reactjs ×1
unit-testing ×1