未定义 React 中的 Jquery

2022-01-31 00:00:00 reactjs jquery javascript ajax

您好,我只是想接收 ajax 请求,但问题是 jquery 没有在 React 中定义.React 版本是 14.0

Hi I just want to receive ajax request, but the problem is that jquery is not defined in React. React version is 14.0

 Uncaught ReferenceError: $ is not defined

我有两个文件:

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>
        );
    }
}

推荐答案

尝试将 jQuery 添加到您的项目中,例如

Try add jQuery to your project, like

npm i jquery --save

或者如果你使用凉亭

bower i jquery --save

然后

import $ from 'jquery'; 

相关文章