28/04/2020
One of the features of ReactJS is JSX. At first glance, it seems that what is the HTML code inside the JavaScript code? But JSX doesn't really mean HTML code inside JavaScript.
JSX - Javascript Extension
This is a special syntax of ReactJS. It's just like HTML. But BabelJS converts this syntax to pure JavaScript code. If we wish, we can create applications using JavaScript code excluding JSX. But in this case our code will be much more verbose. And managing the code will be a lot of trouble.
For example, think about your div, which is -
I am developer
This code will look like this after converting to BabelJS -
React.createElement ('div', null, 'I am div')
From the above example you must understand how easy JSX is and why React has come up with a feature like JSX.
If you want to use JSX, you need to be aware of some of its features.
Such as -
• Wherever you do not use JSX code, you have to import React in that file. If there is no React inside the scope, your compiler will show you an error.
• When you return a JSX from the render method or from a functional component, you can only place one element at the root level. If there are more than one child inside this root element then there is no problem. But only a pea is a JSX element you can return.
• You can use dot notation inside JSX code as you would with a JavaScript object.
You can write any JavaScript code inside the JSX code. For this you need to write the JavaScript code inside the opening curly 6 and the closing curly 6. But remember, you can only execute a JavaScript statement inside. Functions, loops or conditions cannot be used here. However, arrow functions, variable declarations, function calls, conditional operators can be used.
You can store any JSX code inside a variable. You can also return from any function.
So if any other common function generates a view for you, you can call that function inside JSX.
As a child of JSX you can provide any single element or any array. If there is no element then you can provide null. If null is provided as JSX's child then JSX will not render anything.
We can pass props like attributes inside JSX. Again we can pass a Props object inside the JSX using the spread operator.
False False, null, undefined, true simple will be ignored inside JSX. These Boolean values are what we need to render something conditionally.
If we know these things about JSX, we will be able to deal with all kinds of situations. But you have to practice a lot to remember everything.
ReactJS এর অন্যতম একটা ফিচার হচ্ছে JSX. প্রথম দিকে দেখলে মনে হয় জাভাস্ক্রিপ্ট কোডের ভিতরে এইচটিএমএল কোড কি করছে? কিন্তু আসলে JSX মানে জাভাস্ক্রিপ্টের ভিতরে এইচটিএমএল কোড না।
JSX - Javascript Extension
এটা ReactJS এর স্পেশাল একটা সিনট্যাক্স। এটা শুধু দেখতেই এইচটিএমএল এর মত। কিন্তু BabelJS এই সিনট্যাক্স কে কনভার্ট করে পিউর জাভাস্ক্রিপ্ট কোডে রূপান্তর করে দেই। আমরা ইচ্ছে করলেই JSX বাদ দিয়ে শুধু জাভাস্ক্রিপ্ট কোড ব্যবহার করেও অ্যাপ্লিকেশন বানাতে পারি। কিন্তু এই ক্ষেত্রে আমাদের কোডটা অনেক বেশি ভার্বোস হয়ে যাবে। এবং কোড ম্যানেজ করতে অনেক কষ্ট হবে।
উদাহরণ সরূপ নিজের ডিভ টার কথা চিন্তা করা যা -
I am developer
এই কোডটা BabelJS কনভার্ট করার পরে ঠিক এমন হবে দেখতে -
React.createElement('div', null, 'I am div')
ওপরের উদাহরণ থেকে এটা নিশ্চয়ই আপনি বুঝতে পারছেন JSX কতটা সহজ এবং কেন React JSX এর মত একটা ফিচার নিয়ে এসেছে।
JSX ব্যবহার করতে চাইলে এর আরও কিছু বৈশিষ্ট্য সম্পর্কে আপনাদের অবগত থাকতে হবে। যেমন -
• যেখানেই আপনি JSX কোড ব্যবহার করেন না কেন ওই ফাইলে React কে ইম্পোর্ট করে নিতে হবে। যদি স্কোপের ভিতরে React না থাকে তাহলে আপনার কম্পাইলার আপনাকে ইরর শো করবে।
• আপনি render মেথড থেকে অথবা ফাংশনাল কম্পোনেন্ট থেকে যখন কোন JSX রিটার্ন করবেন তখন আপনি রুট লেভেল এ শুধু মাত্র একটি এলিমেন্ট রাখতে পারবেন। এই রুট এলেমেন্টের ভিতরে যদি একাধিক চিলড্রেন থাকে তাহলে কোন প্রব্লেম নেই। কিন্তু শুধু মটর একটি JSX এলিমেন্টই আপনি রিটার্ন করতে পারবেন।
• আপনি JSX কোডের ভিতরে ডট নোটেশন ব্যবহার করতে পারবেন যেমনটা আপনি জাভাস্ক্রিপ্ট অবজেক্টের সাথে করতে পারেন।
• JSX কোডের ভিতরে আপনি যে কোনো জাভাস্ক্রিপ্ট কোড লিখতে পারবেন। এর জন্য আপনাকে ওপেনিং কার্লি { এবং ক্লোসিং কার্লি } এর ভিতরে জাভাস্ক্রিপ্ট কোডটা লিখতে হবে। তবে মনে রাখবেন, এর ভিতরে আপনি শুধু মাত্র একটি জাভাস্ক্রিপ্ট স্টেটমেন্টই এক্সিকিউট করতে পারবেন। ফাংশন, লুপ বা কন্ডিশন এখানে ব্যবহার করা যাবে না। তবে অ্যারো ফাংশন, ভ্যারিয়েবল ডিক্লারেশন, ফাংশন কল, কন্ডিশনাল অপারেটর এই গুলো ব্যবহার করা যাবে।
• আপনি যে কোন JSX কোডকে ভ্যারিয়েবল এর ভিতরে স্টোর করতে পারেন। আবার যে কোন ফাংশন থেকে রিটার্নও করতে পারেন। তাই যদি অন্য কোন সাধারণ ফাংশন আপনার জন্য ভিউ জেনারেট করে থাকে তাহলে সেই ফাংশন টা আপনি JSX এর ভিতরে কল করতে পারবেন।
• JSX এর চিলড্রেন হিসেবে আপনি যে কোনো সিঙ্গেল এলিমেন্ট অথবা যে কোন অ্যারে প্রোভাইড করতে পারেন। যদি কোন এলিমেন্ট না থাকে সেই ক্ষেত্রে আপনি null প্রোভাইড করতে পারেন। যদি JSX এর চাইল্ড হিসেবে null প্রভাইড করা হয় তাহলে JSX কোন কিছুই রেন্ডার করবে না।
• আমরা JSX এর ভিতরে অ্যাট্রিবিউট এর মত করে Props পাস করতে পারি। আবার আমরা একটা Props অবজেক্টকে স্প্রেড অপারেটর ব্যবহার করে JSX এর ভিতরে পাস করতে পারি।
• JSX এর ভিতরে false, null, undefined, true সিম্পলই ইগনোরড হয়ে যাবে। এই Boolean ভ্যালু গুলো আমাদের দরকার হয় কন্ডিশনাল ভাবে কোন কিছু রেন্ডার করার কাজে।
JSX সম্পর্কে এই বিষয় গুলো জানা থাকলেই আমরা সব ধরনের সিসুয়েশন এর মোকাবেলা করতে পারব। তবে সব কিছু মনে রাখার জন্য আপনার প্রচুর প্র্যাকটিস করতে হবে।