我一直不知道我的职能是什么,我不知道为什么?

zed5wv10  于 2022-09-21  发布在  Node.js
关注(0)|答案(2)|浏览(296)

这是我的APP.JSX代码这是当我运行NPM Start src\App.jsx行16:3:‘getFiles’没有定义no-undef时,它不断带来此错误的代码

第33行:1:‘onDrop’未定义no-undef
第43:17行:没有定义‘Web3’-undef
第43:23行:‘Account’没有定义no-undef
第43:42行:‘示例’未定义no-undef

import { EthProvider } from "./contexts/EthContext";
import SolidityDriveContract from "./contracts/SolidityDrive.json";
import Intro from "./components/Intro/";
import Setup from "./components/Setup";
import Demo from "./components/Demo";
import Footer from "./components/Footer";
import "react-drop-zone/dist/styles.css";
import "./App.css";
import {StyledDropZone} from 'react-drop-zone';
import {Table} from "reactstrap";
import {FileIcon, defaultStyles} from 'react-file-icon';
import fileReaderPullStream from 'pull-file-reader';
import ipfs from "./ipfs";

try {
  getFiles = async () => {
    const { account, contract } =this.state;
    let filesLength = await contract.methods.getLength().call({from:account[0]});
    let files = [];
    for (let i = 0; i < filesLength; i++) {
      let file = await contract.methods.getFile(i).call(account[0]);
      files.push(file);  
    }
    this.setState({SolidityDrive: files});

   }

} catch (error) {
  console.log(error);
}

onDrop = async (file) => {
  try {
    const {account, contract} = this.state;
    const stream = fileReaderPullStream(file);
    const result = await ipfs.add(stream); 
  } catch (error) {

  }
}

this.setState({ Web3, account, contract: instance }, this.getFiles);

function App() {

  return (
    <><EthProvider>
      <div id="App">
        <div className="container">
          <Intro />
          <Setup />
          <Demo />
          <StyledDropZone  onDrop={this.onDrop}/>
          <Table>
            <thead>
              <tr>
                <th width="7%" scope="row">Type</th>
                <th>Filename</th>
                <th>Date</th>
              </tr>
            </thead>
            <tbody>
             <tr>
                <th><FileIcon size={30} extension="docx" {...defaultStyles.docx}/></th>
                <th>Filename.docx</th>
                <th>2019.3.17</th>
             </tr>
            </tbody>
          </Table>
          <hr/>
          <Footer />
        </div>
      </div>
    </EthProvider>
    </>

  );
}

export default App;

Package.json文件

{
  "name": "truffle-client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.2.0",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.2.0",
    "ipfs-api": "^26.1.2",
    "pull-file-reader": "^1.0.2",
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
    "react-drop-zone": "^4.3.2",
    "react-file-icon": "^1.2.0",
    "react-moment": "^1.1.2",
    "react-scripts": "^4.0.3",
    "reactstrap": "^9.1.4",
    "web3": "^1.7.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
hc2pp10m

hc2pp10m1#

您正在使用一个功能组件。因此,您必须使用挂钩来设置状态

function App() {
    //set state here
    return()
    }

其次,Try-Catch应该写在后面

function App() {
    //try-catch here
    return()
    }

函数将按如下方式创建(&F)-

const getFiles = async () => { //code here }
const onDrop = async (file) => { //code here }
ie3xauqp

ie3xauqp2#

我想这篇文章会有帮助的。

React Functional Components Const vs Function

const MyComponent = () => {
    return(
      ..
    )
}

v.v.

function MyComponent() {
    return(
      ..
    )
}

相关问题