"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > jQuery를 ReactJS에 통합하여 React \의 구성 요소 기반 아키텍처를 희생하지 않고 아코디언을 구축하려면 어떻게해야합니까?

jQuery를 ReactJS에 통합하여 React \의 구성 요소 기반 아키텍처를 희생하지 않고 아코디언을 구축하려면 어떻게해야합니까?

2025-03-23에 게시되었습니다
검색:546

How can I integrate jQuery into ReactJS to build an accordion without sacrificing React\'s component-based architecture?

reactjs와 jQuery를 인터페이스하는 것 : 종합 가이드

  1. npm install jquery --save
    npm i --save-dev @types/jquery
  2. npm install jquery --save
    npm i --save-dev @types/jquery

Step 1: Understanding the Accordion

import React, { useState } from 'react';

const Accordion = () => {
  const [activeSection, setActiveSection] = useState(null);

  const handleHeadClick = (e) => {
    const section = e.target.parentElement;
    if (section === activeSection) {
      setActiveSection(null);
    } else {
      setActiveSection(section);
    }
  };

  return (
    
{sections.map((section) => (
{section.header}
{section.body}
))}
); };
In its HTML form, an accordion consists of multiple sections, each containing a header and a collapsible body. jQuery는 이벤트 리스너를 사용하여 이러한 요소를 동적으로 조작 할 수 있습니다. line.

명령과 함께 npm을 사용하여 jQuery를 설치하십시오 :

npm jQuery 설치 --save npm i ---save-dev @type/jquery

How can I integrate jQuery into ReactJS to build an accordion without sacrificing React\'s component-based architecture?

jquery를 JSX 파일로 가져 가야합니다. Reactjs

아코디언의 jQuery 코드를 REACTJ로 변환하려면 상태 관리를 사용하여 신체 섹션의 가시성을 처리 할 수 ​​있습니다. 예는 다음과 같습니다.

import React, {usestate} 'react'; const 아코디언 = () => { const [activesection, setActivesection] = usestate (null); const handleheadClick = (e) => { const 섹션 = e.target.parentElement; if (section === activessection) { setActivesection (null); } 또 다른 { setActivesection (섹션); } }; 반품 (
{sections.map ((섹션) => (
{section.header}
{section.body}
))}
); };

[&&)

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3