javascript - 访问 .jsx 元素内的状态
问题描述
如果我有这样的元素:
const CardWebView = () => {
const url = 'xxx';
return (
<WebView
source={{
uri: url,
}}
onNavigationStateChange={this.onNavigationStateChange}
startInLoadingState
javaScriptEnabled
style={{ flex: 1 }}
/>
);
};
例如,我如何使用state
来更改url
?
我试过var url = this.state.url
了,但这给了我一个错误。这部分代码使用箭头函数,我对它们不太熟悉。
解决方案
你应该在功能组件上使用 React Hooks -使用 State Hook - React
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
推荐阅读
- java - 将短路运算符与其他运算符混合
- javascript - JavaScript 脚本不会加载要处理的 JSON 文件的内容
- haproxy - HAProxy SSL 直通和 ACL 规则不起作用
- javascript - 如何将条带结帐中的值插入gtag?
- reactjs - 如何控制多个选择添加和删除选项
- reactjs - 使用和编写 React Hooks
- javascript - 在 Javscript 中,检查(全局)函数是否存在的最佳方法是什么
- python - 具有自适应节点选择的函数的样条逼近
- google-sheets - 如何使 Google Sheet 数组公式与日期函数一起使用
- python - 如何使用 pylatex 创建条码