最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unexpected keyword 'const' in componentDidMount, React - Stack Overflow

programmeradmin1浏览0评论

I'm trying to make a sticky header by getting DOM element and passing a function to it with ponentDidMount, but get an error, that the 'const' is a Unexpected keyword:

ponent:

class Header extends Component {

  ponentDidMount(){
    window.addEventListener('scroll', () => {
      const isTop = window.scrollY > 100,
      const nav = document.getElementById('nav');
      if (isTop) {
        nav.classList.add('scrolled');
      }else {
        nav.classList.add('scrolled');
      }
    });
  }

  ponentWillUnmount() {
    window.removeEventListener('scroll');
  }


  render() {
    return (<>
      <header>
        <nav class="nav" id="nav">
          <ul class="header-list">
            <li>
              <img alt='phone' src={phonelogo} />
            </li>
            <li>123456789</li>
          </ul>
          <ul class="header-list">
            <li>
              <img alt='email' src={email} />
            </li>
            <li>[email protected]</li>
          </ul>
        </nav>
      </header>
    </>)
  };
};


export default Header;

the error:

Line 17:7:  Parsing error: Unexpected keyword 'const'

  15 |     window.addEventListener('scroll', () => {
  16 |       const isTop = window.scrollY > 100,
> 17 |       const nav = document.getElementById('nav');
     |       ^
  18 |       if (isTop) {
  19 |         nav.classList.add('scrolled');
  20 |       }else {

Though, it's probably better to use React refs, but it's still interesting what is going on here.

I'm trying to make a sticky header by getting DOM element and passing a function to it with ponentDidMount, but get an error, that the 'const' is a Unexpected keyword:

ponent:

class Header extends Component {

  ponentDidMount(){
    window.addEventListener('scroll', () => {
      const isTop = window.scrollY > 100,
      const nav = document.getElementById('nav');
      if (isTop) {
        nav.classList.add('scrolled');
      }else {
        nav.classList.add('scrolled');
      }
    });
  }

  ponentWillUnmount() {
    window.removeEventListener('scroll');
  }


  render() {
    return (<>
      <header>
        <nav class="nav" id="nav">
          <ul class="header-list">
            <li>
              <img alt='phone' src={phonelogo} />
            </li>
            <li>123456789</li>
          </ul>
          <ul class="header-list">
            <li>
              <img alt='email' src={email} />
            </li>
            <li>[email protected]</li>
          </ul>
        </nav>
      </header>
    </>)
  };
};


export default Header;

the error:

Line 17:7:  Parsing error: Unexpected keyword 'const'

  15 |     window.addEventListener('scroll', () => {
  16 |       const isTop = window.scrollY > 100,
> 17 |       const nav = document.getElementById('nav');
     |       ^
  18 |       if (isTop) {
  19 |         nav.classList.add('scrolled');
  20 |       }else {

Though, it's probably better to use React refs, but it's still interesting what is going on here.

Share Improve this question asked May 25, 2020 at 13:03 GustėGustė 1373 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You have written:

 const isTop = window.scrollY > 100,
 const nav = document.getElementById('nav');

You need to replace the ma with a simicolon at the end of line 16. Like so:

 const isTop = window.scrollY > 100;
 const nav = document.getElementById('nav');

Line 16. You must remove the Comma or else reomve the const on line 17

发布评论

评论列表(0)

  1. 暂无评论