Is there anyone that could assist me in converting this code from Jquery into a reactjs? I tried a lot, but I kept getting errors.
I had never used jquery before, so I couldn’t understand it.
In simply, this code how its work
If I click "Laptop" text, it will display all 3 images for div "tab1c" and hidden the other div
<div id="tab1c" class="img_group show">
<img src="/images/img1.png" alt=""/>
<img src="/images/img2.png" alt=""/>
<img src="/images/img3.png" alt=""/>
</div>
<div id="tab2c" class="img_group">
<img src="/images/img4.png" alt=""/>
<img src="/images/img5.png" alt="" />
<img src="/images/img6.png" alt="" />
</div>
....
....
and when I press "Phone" this will disable tab1c and show tab2c. like this
<div id="tab1c" class="img_group">
<img src="/images/img1.png" alt=""/>
<img src="/images/img2.png" alt=""/>
<img src="/images/img3.png" alt=""/>
</div>
<div id="tab2c" class="img_group show">
<img src="/images/img4.png" alt=""/>
<img src="/images/img5.png" alt="" />
<img src="/images/img6.png" alt="" />
</div>
showing images depend on Clicked text
jquery:
<section class="hero_sec">
<div class="title">
<h4 id="tab1">Laptop</h4>
<h4 id="tab2">Phone</h4>
<h4 id="tab3">Ipad</h4>
</div>
<div id="tab1c" class="img_group show">
<img src="/images/img1.png" alt=""/>
<img src="/images/img2.png" alt=""/>
<img src="/images/img3.png" alt=""/>
</div>
<div id="tab2c" class="img_group">
<img src="/images/img4.png" alt=""/>
<img src="/images/img5.png" alt="" />
<img src="/images/img6.png" alt="" />
</div>
<div id="tab3c" class="img_group">
<img src="/images/img7.png" alt=""/>
<img src="/images/img8.png" alt="" />
<img src="/images/img39.png" alt="" />
</div>
</section>
<script>
$(document).ready(function(){
$(".title h4").on('click' function()
{
$(this).addClass("active");
// get id
var tab = $(this).attr("id");
console.log(tab);
if($(this).hasClass("active")){
$(this).addclass("active");
$(".title h4").not(this).removeClass("active");
$(".img_group").removeClass("show");
$("#"+ tab + "c").addClass("show");
}}
);
})
</script>
2
Answers
If you want the above code converted to react.js – I would suggest something like this.
You don’t have to do it as jQuery does.
React can do it simpler.