将ReactJS与npm一起使用并给出以下错误(意外令牌)

时间:2016-07-21 06:06:28

标签: reactjs react-jsx npm-install reactjs-native

任何信息都会非常有用,最初我也运行了npm install babel-loader命令。

我有

   ./app-client.js中的错误模块构建失败:SyntaxError:   /Users/dawatramani/ReactNodejs/app-client.js:

意外的令牌(4:13)

  2 | var APP= require('./components/APP');
  3 | 
> 4 | React.render(<APP />,document.getElementById('react-container'));
    |              ^
  5 | 
  

在Parser.pp.raise   (/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/location.js:22:13)       在Parser.pp.unexpected(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:89:8)       在Parser.pp.parseExprAtom(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:522:12)       在Parser.pp.parseExprSubscripts(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:277:19)       在Parser.pp.parseMaybeUnary(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:257:19)       在Parser.pp.parseExprOps(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:188:19)       在Parser.pp.parseMaybeConditional(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:165:19)       在Parser.pp.parseMaybeAssign(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:128:19)       在Parser.pp.parseExprListItem(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:1046:16)       在Parser.pp.parseCallExpressionArguments(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:353:20)       在Parser.pp.parseSubscripts(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:316:31)       在Parser.pp.parseExprSubscripts(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:287:15)       在Parser.pp.parseMaybeUnary(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:257:19)       在Parser.pp.parseExprOps(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:188:19)       在Parser.pp.parseMaybeConditional(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:165:19)       在Parser.pp.parseMaybeAssign(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:128:19)       在Parser.pp.parseExpression(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:92:19)       在Parser.pp.parseStatement(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:163:19)       在Parser.pp.parseBlockBody(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:529:21)       在Parser.pp.parseTopLevel(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/statement.js:36:8)       在Parser.parse(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/parser/index.js:129:19)       在解析(/Users/dawatramani/ReactNodejs/node_modules/babel-core/node_modules/babylon/lib/index.js:47:47)       在File.parse(/Users/dawatramani/ReactNodejs/node_modules/babel-core/lib/transformation/file/index.js:517:34)       在File.parseCode(/Users/dawatramani/ReactNodejs/node_modules/babel-core/lib/transformation/file/index.js:603:20)       at /Users/dawatramani/ReactNodejs/node_modules/babel-core/lib/transformation/pipeline.js:49:12       在File.wrap(/Users/dawatramani/ReactNodejs/node_modules/babel-core/lib/transformation/file/index.js:563:16)       在Pipeline.transform(/Users/dawatramani/ReactNodejs/node_modules/babel-core/lib/transformation/pipeline.js:47:17)       在transpile(/Users/dawatramani/ReactNodejs/node_modules/babel-loader/index.js:14:22)       at Object.module.exports(/Users/dawatramani/ReactNodejs/node_modules/babel-loader/index.js:88:12)

2 个答案:

答案 0 :(得分:2)

对于reactJS的当前版本,您不应使用React.render(),而应使用ReactDOM.render()import ReactDOM from "react-dom"。我认为这将解决您的问题。

ReactDOM.render(<ComponentName />,document.getElementById('react-container'));

这是0.14引入的最新变化。 React现在被拆分为核心库和DOM适配器。渲染现在通过ReactDOM.render完成。

DOCS

答案 1 :(得分:0)

好像你忘了在渲染方法中粘贴一个Component:)

React.render(,document.getElementById('react-container'));