小编Vis*_* DS的帖子

如何从 React Native 中的异步 JS 函数返回变量的值

我正在尝试将外部 URL 加载到 React Native 应用程序中,然后从中提取<title>元素。我不想使用WebView,因为这应该在后台发生。我正在使用dom-parser库来实现它。但是, I 无法返回product_namefrom的值findproduct()。运行该函数时,它确实product_name在控制台中记录了正确的值,表明代码运行良好。但是当我想获取product_name外部的值findproduct()并在其他地方使用时,问题就出现了。

async function findproduct(){
  var DomParser = require('dom-parser');
  var parser = new DomParser()
  const html = (await (await fetch(product)).text()); // html as text
  var dom = parser.parseFromString(html);
  const product_name = dom.getElementsByTagName("TITLE")[0].innerHTML;
  console.log("name="+product_name); //this logs correctly
  return product_name;
}
productname = findproduct(); //this logs something else
console.log(productname);
Run Code Online (Sandbox Code Playgroud)

在这里,productname要么是未定义的,要么是类似{"_U": 0, "_V": 0, "_W": null, "_X": null} …

javascript dom react-native react-native-android

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

Python websocket onclose

WebSocket.onclose()在 javascript 中,当服务器关闭 websocket 连接时,我们必须做一些事情。当客户端设备关闭连接时,有没有办法在 Python 服务器中执行相同的操作?服务器是使用websockets模块本身使用该websockets.serve()方法来运行的。.Connected&都不.ConnectionClosed是属性WebSocketServerProtocol(或者错误是这样说的)。任何帮助,将不胜感激。

javascript python websocket

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

React 状态钩子数组元素不更新状态

我正在尝试减少代码和/或优化模块表单中 React 状态挂钩的使用rn-material-ui-textfield。通常,对于单个文本字段,您可以执行以下操作

import { OutlinedTextField } from 'rn-material-ui-textfield'
// ...and all the other imports

const example = () => {
    let [text, onChangeText] = React.useState('');
    let [error, set_error] = React.useState('');

    const verify = () => {
        if(!text) set_error('Enter a text');
        else console.log('Finished');
    }

    return(
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
            <OutlinedTextField
                onChangeText={onChangeText}
                value={text}
                onSubmitEditing={verify}
                error={error}
              />
        </View>
    );
}
Run Code Online (Sandbox Code Playgroud)

而且肯定没问题。但是,随着您不断添加越来越多的字段,为每个字段设置单独的error钩子text似乎很乏味,并且会生成大量代码。所以,为了防止这种情况,我尝试以不同的方式编写

// ...all imports from previous snippet

const example = () …
Run Code Online (Sandbox Code Playgroud)

javascript arrays reactjs react-native react-hooks

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