반응의 Jquery가 정의되지 않았습니다.
안녕하세요, 저는 단지 ajax 요청을 받고 싶은데 문제는 jquery가 React에 정의되어 있지 않다는 것입니다.리액트 버전은14.0
에러 메시지
Uncaught ReferenceError: $ is not defined
2개의 파일이 있습니다.
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>
);
}
}
추가 시도jQuery프로젝트 진행에 있어서
npm i jquery --save
bower를 사용하는 경우
bower i jquery --save
그리고나서
import $ from 'jquery';
그것은 주로 에서 일어난다.JQuery가 프로젝트에 설치되어 있지 않습니다.패키지 관리자에 따라 다음 명령을 수행하여 JQuery를 프로젝트에 설치합니다.
실
yarn add jquery
npm
npm i jquery --save
그 후 Import만 하면 됩니다.$프로젝트 파일에 저장하십시오. import $ from 'jquery'
저는 단지 ajax 요청을 받고 싶은데, 문제는 jQuery가 React에 정의되어 있지 않다는 것입니다.
그럼 쓰지 마세요.Fetch를 사용하여 IE 11에서 완전히 작동하지 않는 React의 Fetch polyfill을 살펴보고 다른 실행 방법의 예를 확인하십시오.
뭐 이런 거
const that = this;
fetch('http://jsonplaceholder.typicode.com/posts')
.then(function(response) { return response.json(); })
.then(function(myJson) {
that.setState({data: myJson}); // for example
});
다음과 같은 작업을 수행하는 것보다 쉽지 않습니다.
1- 프로젝트에 jquery 설치:
yarn add jquery
2- jquery를 가져오고 DOM을 사용하여 플레이를 시작합니다.
import $ from 'jquery';
참고 사항: 화살표 기능을 사용하는 경우 이 부품 =에 해당하는 Const는 필요하지 않습니다.다음과 같은 경우가 있습니다.
fetch('http://jsonplaceholder.typicode.com/posts')
.then((response) => { return response.json(); })
.then((myJson) => {
this.setState({data: myJson}); // for example
});
h1 태그에 "ref" 추가:
<h1 ref="source">Hey there.</h1>
그리고.
const { source } = this.props;로 바꾸다.const { source } = this.refs;
언급URL : https://stackoverflow.com/questions/33351288/jquery-in-react-is-not-defined
'programing' 카테고리의 다른 글
| 루비에서 파일을 읽는 일반적인 방법은 무엇입니까? (0) | 2023.06.04 |
|---|---|
| 날짜(현재 시간 하루 전)를 Bash로 가져옵니다. (0) | 2023.06.04 |
| 드롭다운 목록의 첫 번째 요소 비우기 (0) | 2023.04.05 |
| JavaScript에서 응답 본문을 다시 읽습니다. (0) | 2023.04.05 |
| 디스패치가 완료될 때까지 리덕스 액션을 기다립니다. (0) | 2023.04.05 |