无法找到错误

时间:2017-04-05 21:03:38

标签: reactjs unexpected-token

我知道这是一个未成年人,也许是一个愚蠢的问题,但我在一个错误中停留了大约一个小时,我无法看到。这是我的代码:

const ModalRoot = ({ modalType, modalProps, locale }) => {
  if (!modalType) {
    return <span />;
  }

  return (
    <IntlProvider
      locale={locale}
      key={locale}
      messages={messagesFor(locale)}
    >
      <div className="backdrop">
        {renderAppropriateModal(modalType, modalProps)}
      </div>
    </IntlProvider>
  );
};

控制台在if say意外令牌中显示错误。为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

这可能是你的错误...

检查这些样本。

申报错误的地方。

import React from 'react';

class YOURCLASS extends React.Component {
   constructor(props) {
     super(props);

   }

   //do not place this ModalRoot here
   const ModalRoot = ({ modalType, modalProps, locale }) => {
      //contents
   }

   render(){
     return(
        <div>{yourContent}</div>
     );
   }
}

声明的正确位置

import React from 'react';

//Place it here outside the class YOURCLASS
const ModalRoot = ({ modalType, modalProps, locale }) => {
      //contents
}

class YOURCLASS extends React.Component {
   constructor(props) {
     super(props);

   }



   render(){
     return(
        <div>{yourContent}</div>
     );
   }
}

如果你还打算在课堂上这样做...更好地使用功能......

import React from 'react';

class YOURCLASS extends React.Component {
   constructor(props) {
     super(props);

   }

   //function type ModalRoot
   ModalRoot(modalType, modalProps, locale){
      //contents
      return <IntlProvider />;
   }

   render(){
     const {modalType, modalProps, locale} = this.props;
     let yourContent = this.ModalRoot(modalType, modalProps, locale);
     return(
        <div>{yourContent}</div>
     );
   }
}
希望它有所帮助...