누군가 React로 나를 도울 수 있습니까?

3807 단어 reactjavascripthelp
REACT 앱에서 초기화되기 전에 const-variable에 어떻게 액세스할 수 있습니까? (Chrome 콘솔에는 없습니다!)

//MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init
  • 반응 앱

  • import React from "react";
    import "./styles.css";
    
    try {
      console.log(test); // undefined
      const test = "THIS IS FOR TEST";
    } catch (error) {
      // I don't understand why it doesn't throw the reference error.. 😫
      console.log(error);
    }
    
    export default function App() {
      return (
        <div className="App">
          <h1>Hello CodeSandbox</h1>
          <h2>Start editing to see some magic happen!</h2>
        </div>
      );
    }
    

  • 크롬 콘솔

  • try {
      console.log(test);
      const test = "THIS IS FOR TEST";
    } catch (error) {
      console.log(error);
      // ReferenceError: Cannot access 'test' before initialization
        at <anonymous>:2:15
    }
    

    좋은 웹페이지 즐겨찾기