我一直在我的函数组件中使用 useRef 作为类实例变量的替代品,我真的很喜欢它。所以通常情况是这样的:
首先声明变量,scrollRef例如:
const scrollRef = useRef()
Run Code Online (Sandbox Code Playgroud)
然后,将某些 ref 的值分配给scrollRef.current:
<ScrollView ref={ref => { scrollRef.current = ref }}>
// (Yeah, I'm doing mostly React Native)
Run Code Online (Sandbox Code Playgroud)
最后,使用参考:
<TouchableOpacity onPress={() => scrollRef.current.scrollToEnd()} />
Run Code Online (Sandbox Code Playgroud)
这很好用。但是当我有很多useRefs 时,似乎有两个问题:
.current到处都是.current。所以我想知道是否有更好的方法。
let我想到用而不是声明变量const,然后直接赋值给.current:
let myRef = useRef().current
Run Code Online (Sandbox Code Playgroud)
然后直接读取并变异myRef
这样做有什么缺点,是否存在行不通的情况?有没有更好的解决方案来避免.current到处使用?
我正在对 GitHub 问题发表评论,并希望建议对之前评论中发布的代码进行编辑。如何格式化我的降价以突出显示要以红色删除的代码和要以绿色添加的代码,就像在 git commit 更改摘要中一样?
我以前做过这个,但可能不是在 GitHub 上,所以这可能吗?
我开始使用 Appium 测试我的 React Native 应用程序。我有一个简单的登录场景,我希望应用程序在输入并提交一些用户名和密码后status出现。'Logged in'我正在 iPhone X 12.2 模拟器上运行测试。
但是测试失败并出现以下错误:
Expected: "Logged in"
Received: "status"
Run Code Online (Sandbox Code Playgroud)
不知何故,文本值未正确接收。那么如何获取我的元素的内部文本呢?
应用程序.js:
<Text accessibilityLabel="status">{this.state.status}</Text>
Run Code Online (Sandbox Code Playgroud)
appium.test.js:
test('Login success', async() => {
expect(await driver.hasElementByAccessibilityId('username input')).toBe(true)
expect(await driver.hasElementByAccessibilityId('password input')).toBe(true)
expect(await driver.hasElementByAccessibilityId('submit button')).toBe(true)
expect(await driver.hasElementByAccessibilityId('status')).toBe(true)
await driver.elementByAccessibilityId('username input').sendKeys('some_username')
await driver.elementByAccessibilityId('password input').sendKeys('some_password')
await driver.elementByAccessibilityId('submit button').click()
const result = await driver.elementByAccessibilityId('status').text()
console.log(result) // 'status' WHY???
// the test runs fine until here:
expect(result).toBe('Logged in')
})
Run Code Online (Sandbox Code Playgroud)
我能想到的是,这text()不是获取元素内部文本的正确函数,但这是我在我一直关注的文档或教程中看到的所有内容......
我有一个对象数组,每个对象具有三个属性(年、总计、per_capita)。例子:
0: Object
per_capita: "125.8"
total: "1007.2"
year: "2009"
Run Code Online (Sandbox Code Playgroud)
这些属性是字符串,我想创建一个遍历数组并将它们转换为 int 的循环。我尝试了以下循环:
for (i=0; i<data.length; i++){
parseInt(data[i].year, 10)
parseInt(data[i].total, 10)
parseInt(data[i].per_capita, 10)
}
Run Code Online (Sandbox Code Playgroud)
但是,当我执行 typeof(data[0].total) 时,它说它是一个字符串。我在程序的后期遇到了问题,我认为这是因为无法正确计算值,因为它们的类型不正确。任何人都知道问题出在哪里?
我有一个具有以下结构的 monorepo:
\nrepo/\n\xe2\x94\x9c\xe2\x94\x80 node_modules/\n\xe2\x94\x9c\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80 packages/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 design-system/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 package.json // dependencies installed in repo/node_modules\n\xe2\x94\x9c\xe2\x94\x80 apps/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 my-app/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 node_modules/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 package.json // dependencies installed in my-app/node_modules\n\nRun Code Online (Sandbox Code Playgroud)\nrepo/packages 包含 repo/apps 中各种应用程序使用的共享包。共享包的依赖项位于 repo/node_modules 中,通过顶层 package.json 中的 Yarn Workspaces 进行管理:
\nrepo/\n\xe2\x94\x9c\xe2\x94\x80 node_modules/\n\xe2\x94\x9c\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80 packages/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 design-system/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 package.json // dependencies installed in repo/node_modules\n\xe2\x94\x9c\xe2\x94\x80 apps/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 my-app/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 node_modules/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80 package.json // dependencies installed in my-app/node_modules\n\nRun Code Online (Sandbox Code Playgroud)\nrepo/apps 包含 React Native 应用程序。repo/apps 中的应用程序有自己的本地 node_modules,并且不是 …
react-native ×2
reactjs ×2
appium ×1
arrays ×1
dependencies ×1
git ×1
javascript ×1
markdown ×1
monorepo ×1
object ×1
parseint ×1
react-hooks ×1
testing ×1
typescript ×1