Javascript 对本地和Expo FS导入问题作出反应

Javascript 对本地和Expo FS导入问题作出反应,javascript,reactjs,expo,fs,Javascript,Reactjs,Expo,Fs,这一行: var a=要求('react-native-fs') 返回以下错误: JSX值应该是表达式或引用的JSX文本(53:22) 谢谢你的帮助 import React from 'react'; import { StyleSheet, View, Text, TextInput, } from 'react-native'; export default class Component1 ext

这一行: var a=要求('react-native-fs')

返回以下错误: JSX值应该是表达式或引用的JSX文本(53:22)

谢谢你的帮助

import React from 'react';
    import { 
        StyleSheet,
        View,
        Text,
        TextInput,
     } from 'react-native';
    export default class Component1 extends React.Component {
        constructor(props) {
            super(props);

            this.state = {
        textInputValue: "",
    }
}
render() {
    if (!this.props.visible) {
        return false;
    }     
    return (

        <View 
            style={styles.component}
        >
            <View style={styles.layouts}>
                <View style={styles.layout1}>
                    <View style={styles.itemcontainer1}>
                        <View style={styles.itemcontainer1Inner}>
                            <View style={styles.item1}>
                                    <TextInput 
                                        style={styles.item1TextInput}
                                        placeholder={"b"}
                                        underlineColorAndroid={"transparent"}
                                        placeholderTextColor={"rgba(0,0,0,1)"}
                                        onChangeText={(val) => this.setState({ textInputValue: val })}
                                        value={this.state.textInputValue}
                                        var a = require"react-native-fs";
                                        var path = a.DocumentDirectoryPath + '../textfile.txt';
                                        a.writeFile(path, this.state.textInputValue, 'utf8');
                                            .then((success) => {
                                                console.log('File Written');
从“React”导入React;
导入{
样式表,
看法
文本
文本输入,
}从“反应本机”;
导出默认类Component1扩展React.Component{
建造师(道具){
超级(道具);
此.state={
textInputValue:“”,
}
}
render(){
如果(!this.props.visible){
返回false;
}     
返回(
this.setState({textInputValue:val})}
value={this.state.textInputValue}
var a=需要“反应本机fs”;
var path=a.DocumentDirectoryPath+'../textfile.txt';
a、 writeFile(路径,this.state.textInputValue,'utf8');
。然后((成功)=>{
log(“文件写入”);
是react应用程序常用的javascript的扩展。它看起来类似于html标记,在元素的开始和结束处有尖括号,并且在这些元素上有属性。如果您想将普通javascript放在JSX中,可以使用花括号。您有这样的示例,例如这一个,它有一些视图的JSX,然后切换回javascript以传入styles.layouts:

<View style={styles.layouts}>
或者,如果该代码应该在他们单击按钮时运行,您可以这样做:

onButtonPressed() {
    var path = fs.DocumentDirectoryPath + '../textfile.txt';
    fs.writeFile(path, this.state.textInputValue, 'utf8')
    //etc
}

render() {
  return (
    // other components omitted for brevity
    <Button onPress={() => this.onButtonPressed()}/>
  )
}
onButtonPressed(){
var path=fs.DocumentDirectoryPath+'../textfile.txt';
fs.writeFile(路径,this.state.textInputValue,“utf8”)
//等
}
render(){
返回(
//为简洁起见,省略了其他组件
此.onButtonPressed()}/>
)
}

除非该行以某种方式嵌入到JSX表达式中,否则它不是错误的原因。您能向我们展示更多信息吗?您的意思是什么?我是编程新手。您展示给我们的代码不是问题所在。为了帮助您,我们需要查看更多代码。错误表明问题出现在第53行,因此请向我们展示该行和代码正在解释它。我编辑了问题以包含周围的代码。谢谢,这有足够的信息来回答问题。我很快会发布一个。感谢您的帮助。我将测试此问题并尽快回复!希望它运行良好。我会再次检查,但我还有一些其他需要做的事情,因此我可能不会及时回复。
import fs from 'react-native-fs';

export default class Component1 extends React.Component {
  componentDidMount() {
    var path = fs.DocumentDirectoryPath + '../textfile.txt';
    fs.writeFile(path, this.state.textInputValue, 'utf8')
        .then((success) => {
            console.log('File Written');
            // Possibly call this.setState if you want Component1 to render with something different now that the write is complete
        })
  }

  render() {
    // similar render to before, but without the file system code
  }
}
onButtonPressed() {
    var path = fs.DocumentDirectoryPath + '../textfile.txt';
    fs.writeFile(path, this.state.textInputValue, 'utf8')
    //etc
}

render() {
  return (
    // other components omitted for brevity
    <Button onPress={() => this.onButtonPressed()}/>
  )
}