2015-10-26 45 views
26

嗨,我只是想接收ajax请求,但问题是jQuery没有在React中定义。反应过来的版本是14.0React中的Jquery未定义

错误消息

Uncaught ReferenceError: $ is not defined 

我有两个文件

index.js

import React from 'react'; 
import { render } from 'react-dom'; 
import App from './containers/App'; 

const root = document.getElementById('root'); 

render( 
    <App source='https://api.github.com/users/octocat/gists' />, 
    root 
); 

app.js

import React, { Component } from 'react'; 

export default class App extends Component { 

componentDidMount() { 
    const { source } = this.props; 

    console.log($); // throws error 
} 

render() { 
    return (
     <h1>Hey there.</h1> 
    ); 
} 
} 
+3

我看不到您在该代码中的任何位置定义jQuery。你为什么认为它应该工作? – Quentin

回答

138

试加jQuery到您的亲JECT,像

npm i jquery --save 

,或者如果您使用的凉亭

bower i jquery --save 

然后

import $ from 'jquery'; 
+48

为了上帝的爱,接受这个答案 – skwidbreth

-5

添加 “裁判” 到h1标签:

<h1 ref="source">Hey there.</h1> 


const { source } = this.props;更改为const { source } = this.refs;