简体   繁体   English

我的功能一直未定义,我不知道为什么?

[英]i keep getting undefined for my functions and i dont know why?

THIS IS MY APP.JSX CODE This is the code it keeps bringing this error when I run npm start src\App.jsx Line 16:3: 'getFiles' is not defined no-undef这是我的 APP.JSX 代码这是当我运行 npm start src\App.jsx 第 16:3 行:'getFiles' is not defined no-undef
Line 33:1: 'onDrop' is not defined no-undef第 33:1 行:'onDrop' 未定义 no-undef
Line 43:17: 'Web3' is not defined no-undef第 43:17 行:“Web3”未定义 no-undef
Line 43:23: 'account' is not defined no-undef第 43:23 行:“帐户”未定义 no-undef
Line 43:42: 'instance' is not defined 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;

the package.json file 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"
    ]
  }
}

You are using a functional component.您正在使用功能组件。 So, you have to use hooks for setting the state因此,您必须使用挂钩来设置 state

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

Secondly, try-catch should be written after其次,try-catch应该写在后面

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

& functions will be created like this - & 函数将像这样创建 -

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

I think this article will help.我认为这篇文章会有所帮助。

React Functional Components Const vs Function React 功能组件 Const 与 Function

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

vs.对比

function MyComponent() {
    return(
      ..
    )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道为什么函数不使用我的变量 - i dont know why the functions aren´t using my variable 不断收到TypeError:当我单击复选框时无法读取未定义的属性“地图”,不知道问题出在哪里 - Keep getting TypeError: Cannot read property 'map' of undefined when I click on checkbox, dont know where the problem is 我不知道为什么我在控制台中未定义 #javascript 试图创建牛顿第一个方程的计算器 - i dont know why i am getting undefined in console #javascript trying to create calculator of newton's first equation 我不知道为什么我的代码是错误的? 有什么问题吗? - I dont know why my code is wrong ? and what wrong is it? 我不知道为什么我的 setInterval 不起作用 javascript - I dont know why my setInterval is not working javascript 我的验证表 Dosent 有效,但我不知道为什么? - my validation Form Dosent work and i dont know why? 我的 displayCart() 方法似乎永远不会执行,我不知道为什么 - My displayCart() method never seems to execute and I dont know why 我的汉堡菜单不起作用 我不知道为什么 - My hamburger menu isnt working I dont know why 我的 p5.js 代码滞后我不知道为什么? - My p5.js code is lagging i dont know why? 为什么我不断得到$(...)。scrollSpy是未定义的? - Why do I keep getting $(…).scrollSpy is undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM