json 当我尝试初始化它时,我的npm不会正确运行,我希望这不是问题,有人能检查一下我的工作吗?它没有渲染

drkbr07n  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(70)

所以我的npm和json从认证一开始就不起作用。我的应用程序有两个同行评论,一个说所有功能都在那里,另一个说它完全无法使用。我试图解决这里的所有问题,但我找不到问题所在。请帮助?

import {
    useRef, 
    useState
} from "react";
import "./App.css";

function App() {
    const inputRef = useRef{null};
    const resultRef = useRef{null};
    const [result, setResult] = useState{0};

    const plus = {e} => {
        e.preventDefault();
        setResult((result) => result + Number (inputRef.current.vaule));
    };

    const minus = (e) => {
        e.preventDefault();
        setResult((result) => result - Number (inputRef.current.value));
    };

    const times = (e) => {
        e.preventDefault();
        setResult((result) => result * Number (inputRef.current.value));
    }

    const divide = (e) => {
        e.preventDefault();
        setResult ((result) => result / Number (inputRef.current.value));
    }

    const resetInput = (e) => {
        e.preventDefault();
        inputRef.current.value = 0;
    };

    const resetResult = (e) => {
        e.preventDefault();
        setResult((preVal => preVale * 0))
    }

    reutrn (
        <div className="App">
            <div>
                <h1>Simplest Working Calculator</h1>
            </div>
            <form>
                <p ref={resultRef}>result</p>
                <input
                    pattern="[0-9]"
                    ref={inputRef}
                    type="number"
                    placeholder="Type a number"
                    />
                <button onClick={plus}>add</button>
                <button onClick={minus}>subtract</button>
                <button onClick={times}>multiply</button>
                <button onClick={divide}>divide</button>
                <button onClick={resetInput}>reset input</button>
                <button onClick={resetResult}>reset result</button>
            </form>
        </div>
    )

};

字符串

frebpwbc

frebpwbc1#

尝试下面的代码,有一些错别字,一个主要的是reutrn而不是return
另一个你可能想检查的是你的钩子初始化:
const [result, setResult] = useState{0}; <--错误
const [result, setResult] = useState(0); <--右

import { useRef, useState } from "react";
import "./App.css";

function App() {
  const inputRef = useRef(null);
  const resultRef = useRef(null);
  const [result, setResult] = useState(0);

  const plus = (e) => {
    e.preventDefault();
    setResult((result) => result + Number(inputRef.current.value));
  };

  const minus = (e) => {
    e.preventDefault();
    setResult((result) => result - Number(inputRef.current.value));
  };

  const times = (e) => {
    e.preventDefault();
    setResult((result) => result * Number(inputRef.current.value));
  };

  const divide = (e) => {
    e.preventDefault();
    setResult((result) => result / Number(inputRef.current.value));
  };

  const resetInput = (e) => {
    e.preventDefault();
    inputRef.current.value = 0;
  };

  const resetResult = (e) => {
    e.preventDefault();
    setResult((preVal) => preVal * 0);
  };

  return (
    <div className="App">
      <div>
        <h1>Simplest Working Calculator</h1>
      </div>
      <form>
        <p ref={resultRef}>{result}</p>
        <input
          pattern="[0-9]"
          ref={inputRef}
          type="number"
          placeholder="Type a number"
        />
        <button onClick={plus}>add</button>
        <button onClick={minus}>subtract</button>
        <button onClick={times}>multiply</button>
        <button onClick={divide}>divide</button>
        <button onClick={resetInput}>reset input</button>
        <button onClick={resetResult}>reset result</button>
      </form>
    </div>
  );
}

export default App;

字符串

相关问题