2015-10-26 47 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;