Passing array of arrays as props - reactjs

Can someone explain what I am doing wrong here....
<Something
content={[
{
heading: "Add Company",
icon: "plus",
options: {[
{
image: "srcimage",
text: "New Company",
link: "test"
}, { //Errors here
image: "srcimage",
text: "Existing Company",
link: "test"
}
]}
}, {
heading: "Import Company",
icon: "download",
options: {[
{
image: "/src/image",
text: "Class",
link: "/test"
},
{
image: "/src/image",
text: "BGL",
link: "/test"
},
{
image: "/src/image",
text: "SuperMate",
link: "/test"
}
]}
}]
} />
I get the error... Unexpected token, expected "]" where it says error here. Eventually I would like to create some blocks of content based on whats passed in like this....
Thanks

{[]} isn't valid syntax for an object.
content={[
{
heading: "Add Company",
icon: "plus",
options: [ // remove the curly boy that was here
{
image: "srcimage",
text: "New Company",
link: "test"
}, {
image: "srcimage",
text: "Existing Company",
link: "test"
}
] // and here
}, {
heading: "Import Company",
icon: "download",
options: [ // and here
{
image: "/src/image",
text: "Class",
link: "/test"
},
{
image: "/src/image",
text: "BGL",
link: "/test"
},
{
image: "/src/image",
text: "SuperMate",
link: "/test"
}
] // and here
}]}

For the options properties you need to remove the curly braces, just pass the array
options: [
{
image: "srcimage",
text: "New Company",
link: "test"
},
{
image: "srcimage",
text: "Existing Company",
link: "test"
}
]

Related

I am struggling with managing the state of a react form

i have this json object which i use to map the form. I have a lot of nested questions. It is used for medical purposes. Here's an portion of the json
parts: [
{
title: "Surgery-related",
questions: [
{
type: "radio",
required: true,
label: "Timing of surgery",
name: "timing_of_surgery",
options: [
{
label: "Emergent non-cardiac surgery",
value: "emergent_non_cardiac_surgery",
},
{
label: "Urgent non-cardiac surgery",
value: "urgent_non_cardiac_surgery",
},
{
label: "Time-sensitive non-cardiac surgery",
value: "time_sensitive_non_cardiac_surgery",
},
{
label: "Elective non-cardiac surgery",
value: "elective_non_cardiac_surgery",
name: "elective_non_cardiac_surgery",
nestedQuestion: {
type: "radio",
options: [
{
label: "Possible to defer non-cardiac surgery",
value: "possible_to_defer_non_cardiac_surgery",
},
{
label: "Not possible to defer non-cardiac surgery",
value: "not_possible_to_defer_non_cardiac_surgery",
},
],
},
},
],
},
{
type: "select",
required: true,
label: "Type of surgery or intervention",
name: "type_of_surgery_or_intervention",
options: [
{
value: "Low surgical risk (<1%)",
titles: [
"Breast",
"Dental",
"Endocrine: thyroid",
"Eye",
"Gynaecological: minor",
"Orthopaedic minor (meniscectomy)",
"Reconstructive",
"Superficial surgery",
"Urological minor: (transurethral resection of the prostate)",
"Video assisted thoracic surgery minor lung resection",
],
},
{
value: "Intermediate surgical risk (1-5%)",
titles: [
"Carotid asymptomatic (coronary endarterectomy or coronary artery stenting)",
"Carotid symptomatic (coronary endarterectomy)",
"Endovascular aortic aneurysm repair",
"Head or neck surgery",
"Intraperitoneal: splenectomy, hiatal hernia repair, cholecystectomy",
"Intrathoracic: non-major",
"Neurological or orthopaedic: major (hip and spine surgery)",
"Peripheral arterial angioplasty",
"Renal transplants",
"Urological or gynaecological: major",
],
},
{
value: "High surgical risk (>5%)",
titles: [
"Adrenal resection",
"Aortic and major vascular surgery",
"Carotid symptomatic (Carotid artery stenting)",
"Duodenal-pancreatic surgery",
"Liver resection, bile duct surgery ",
"Oesophagectomy",
"Open lower limb revascularization for acute limb ischaemia or amputation",
"Pneumonectomy (video-assisted thoracic surgery or open surgery)",
"Pulmonary or liver transplant",
"Repair of perforated bowel",
"Total cystectomy",
],
},
],
},
{
type: "select",
required: true,
label: "Bleeding risk",
name: "bleeding_risk",
options: [
{
value: "Minor bleeding risk",
titles: [
"Cataract or glaucoma procedure",
"Dental procedures: extractions (1–3 teeth), periodontal surgery, implant positioning endodontic (root canal) procedures, subgingival scaling/cleaning",
"Endoscopy without biopsy or resection",
" Superficial surgery (e.g. abscess incision, small skin excisions/ biopsy)",
],
},
{
value: "Low bleeding risk",
titles: [
"Abdominal surgery: cholecystectomy, hernia repair, colon resection",
"Breast surgery",
"Complex dental procedures (multiple tooth extractions)",
"Endoscopy with simple biopsy",
"Gastroscopy or colonoscopy with simple biopsy",
"Large-bore needles procedures (e.g. bone marrow or lymph node biopsy)",
"Non-cataract ophthalmic surgery",
"Small orthopaedic surgery (foot, hand arthroscopy)",
],
},
{
value: "High bleeding risk",
titles: [
"Abdominal surgery with liver biopsy, extracorporeal shockwave lithotripsy",
"Extensive cancer surgery (e.g. pancreas, liver)",
"Neuraxial (spinal or epidural) anaesthesia",
"Neurosurgery (intracranial, spinal) ",
"Major orthopaedic surgery ",
"Procedures with vascular organ biopsy (kidney or prostate)",
"Reconstructive plastic surgery",
"Specific interventions (colon polypectomy, lumbar puncture, endovascular aneurysm repair)",
"Thoracic surgery, lung resection surgery",
" Urological surgery (prostatectomy, bladder tumour resection)",
"Vascular surgery (e.g. abdominal aortic aneuvrysm repair, vascular bypass)",
],
},
],
},
],
},
{
title: "Patient-related",
questions: [
{
type: "input",
input_type: "text",
placeholder: "Enter patient's name",
required: true,
label: "Patient's name",
name: "patient_name",
},
{
type: "checkbox",
required: false,
label: "Cardiovascular risk factor",
name: "cardiovascular_risk_factor",
options: [
{
label: "Hypertension",
value: "hypertension",
},
{
label: "Diabetes",
value: "diabetes",
},
{
label: "Dyslipidemia",
value: "dyslipidemia",
},
{
label: "Smoking",
value: "smoking",
},
{
label: "Age greater than or equal to 65 years",
value: "age_greater_than_65_years",
},
{
label: "Family history of cardiovascular disease",
value: "family_history_of_cardiovascular_disease",
required: true,
nestedQuestion: {
type: "checkbox",
options: [
{
label:
"Myocardial infarction or sudden death <55 years with father or brother",
value:
"myocardial_infarction_or_sudden_death_less_than_55_years_with_father_or_brother",
},
{
label:
"Myocardial infarction or sudden death <65 years with mother or sister",
value:
"myocardial_infarction_or_sudden_death_less_than_65_years_with_mother_or_sister",
},
{
label:
"Cerebrovascular accident <45 years with parents or brother/sister",
value:
"cerebrovascular_accident_less_than_45_years_with_parents_or_brother_sister",
},
],
},
},
],
},
{
type: "checkbox",
label: "Antecedents",
name: "antecedents",
sections: [
{
title: "Cardiovascular disease ",
options: [
{
label: "Heart failure",
value: "heart_failure",
namee: "heart_failure",
nestedQuestion: {
type: "radio",
options: [
{
label: "Stable heart failure",
value: "stable_heart_failure",
},
{
label: "Unstable heart failure",
value: "unstable_heart_failure",
},
],
},
},
{
label: "Coronary artery disease",
value: "coronary_artery_disease",
},
{
label: "Vasopastic angina",
value: "vasopastic_angina",
},
{
label: "Mechanical prosthetic valve",
value: "mechanical_prosthetic_valve",
name: "mechanical_prosthetic_valve",
nestedQuestion: {
type: "checkbox",
options: [
{
label:
"Mechanical aortic valve replacement(AVR), associated with ",
value:
"mechanical_aortic_valve_replacement_associated_with",
name: "mechanical_aortic_valve_replacement_associated_with",
nestedQuestion: {
type: "checkbox",
options: [
{
label: "Atrial fibrillation",
value: "atrial_fibrillation1",
},
{
label: "Previous thromboembolism, ",
value: "previous_thromboembolism",
},
{
label: "Severe left ventricular dysfunction ",
value: "severe_left_ventricular_dysfunction",
},
{
label: "Hypercoagulable state",
value: "hypercoagulable_state",
},
]
}
},
{
label:
"Older-generation mechanical aortic valve replacement ",
value:
"older_generation_mechanical_aortic_valve_replacement",
},
{
label: "Mechanical mitral valve replacement",
value: "mechanical_mitral_valve_replacement",
},
],
},
},
{
label: "Atrial fibrillation",
value: "atrial_fibrillation",
},
{
label: "Other rythmic antecedents",
value: "other_rythmic_antecedents",
},
{
label: "Venous thromboembolism",
value: "venous_thromboembolism",
},
],
},
{
title: "Non cardiovascular disease",
options: [
{
label: "Diabetes mellitus",
value: "diabetes_mellitus",
},
{
label: "Renal failure",
value: "renal_failure",
},
],
},
],
},
],
},
{
title: "Examination",
questions: [
{
type: "checkbox",
required: true,
label: "Examination",
name: "examination",
options: [
{
label: "Asymptomatic",
value: "asymptomatic",
default: true,
},
{
label: "Newly detected murmurs",
value: "newly_detected_murmurs",
},
{
label: "Chest pain",
value: "chest_pain",
},
{
label: "Symptoms/signs suggestive of cardio-vascular disease",
value: "symptoms_signs_suggestive_of_cardiovascular_disease",
},
{
label:
"Poor functional capacity (METs<4 –if the patient cannot climb two flights of stairs-)",
value: "poor_functional_capacity",
},
{
label: "High clinical risk factor : (RCRI >= 1)",
value: "high_clinical_risk_factor",
name: "high_clinical_risk_factor",
required: true,
nestedQuestion: {
type: "checkbox",
options: [
{
label: "Ischaemic heart disease",
value: "ischaemic_heart_disease",
},
{
label: "Cerebrovascular disease",
value: "cerebrovascular_disease",
},
{
label: "History of congestive heart failure",
value: "history_of_congestive_heart_failure",
},
{
label: "Insulin therapy for diabetes",
value: "insulin_therapy_for_diabetes",
},
{
label: "Serum creatinine level ≥2 mg/dL",
value:
"serum_creatinine_level_greater_than_or_equal_to_2_mg_per_dL",
},
{
label: "High-risk surgery",
value: "high_risk_surgery",
},
],
},
},
{
label: "Abnormal ECG",
value: "abnormal_ecg",
name: "abnormal_ecg",
required: true,
nestedQuestion: {
type: "checkbox",
options: [
{
label: "Pathological Q wave",
value: "pathological_q_wave",
},
{
label: "ST-T wave changes",
value: "st_t_wave_changes",
},
{
label: "Non-sinus rhythm",
value: "non_sinus_rhythm",
},
{
label: "Left bundle branch block",
value: "left_bundle_branch_block",
},
],
},
},
{
label: "High NT-pro-BNP/BNP",
value: "high_nt_pro_bnp_bnp",
},
{
label: "Acute coronary syndrome",
value: "acute_coronary_syndrome",
},
],
},
],
},
],
The way i render this form is using recursion. I have this jsx component named Question.jsx which i pass with it the question. Where i'm stuck is to manage the state of the hole form in one useState hook object. And espacialy when it comes to nested questions with levels of more the 2. As i need to update the parent. I realy need help please !

How to filter out unique tags and store in an array from object given below using hooks

export const imageTag = [
{
link:
"https://images.pexels.com/photos/70069/pexels-photo-70069.jpeg?cs=srgb&dl=pexels-daniyal-ghanavati-70069.jpg&fm=jpg",
id: 1,
title: "Bird image",
tags: ["nature", "bird"]
},
{
link:
"https://media.istockphoto.com/photos/renewable-energy-and-sustainable-development-picture-id1186330948?k=20&m=1186330948&s=612x612&w=0&h=5aNPCcQ8FcZraX44PEhb2mqcHkow2xMITJMHdh28xNg=",
id: 2,
title: "nature 1",
tags: ["nature"]
},
{
link:
"https://images.unsplash.com/photo-1538941214074-9b578e573632?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8Nnx8fGVufDB8fHx8&w=1000&q=80",
id: 3,
title: "river image",
tags: ["nature", "river"]
},
{
link:
"https://images.pexels.com/photos/459198/pexels-photo-459198.jpeg?cs=srgb&dl=pexels-pixabay-459198.jpg&fm=jpg",
id: 4,
title: "image river-nature",
tags: ["nature", "river"]
},
{
link:
"https://images.pexels.com/photos/1001682/pexels-photo-1001682.jpeg?cs=srgb&dl=pexels-kellie-churchman-1001682.jpg&fm=jpg",
id: 5,
title: "image12",
tags: ["sea"]
},
{
link:
"https://upload.wikimedia.org/wikipedia/commons/thumb/7/7d/Wildlife_at_Maasai_Mara_%28Lion%29.jpg/800px-Wildlife_at_Maasai_Mara_%28Lion%29.jpg",
id: 6,
title: "Animal 1",
tags: ["animal"]
},
{
link:
"https://res.cloudinary.com/dk-find-out/image/upload/q_80,w_1920,f_auto/Animal_kingdom_nzwbda.jpg",
id: 7,
title: "Animal 2",
tags: ["animal"]
},
{
link:
"https://image.shutterstock.com/image-photo/side-view-lion-walking-panthera-260nw-165448391.jpg",
id: 8,
title: "lion 2",
tags: ["lion"]
},
{
link:
"https://images.pexels.com/photos/1172253/pexels-photo-1172253.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
id: 9,
title: "lifestyle image",
tags: ["lifestyle", "river"]
},
{
link:
"https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569__480.jpg",
id: 10,
title: "galaxy 1",
tags: ["galaxy"]
},
{
link:
"https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bGVuc3xlbnwwfHwwfHw%3D&w=1000&q=80",
id: 11,
title: "Lens",
tags: ["lens"]
},
{
link:
"https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510__480.jpg",
id: 12,
title: "image flower",
tags: ["nature", "flower"]
}
];
here is one approach.
const data = [{ link: "https://images.pexels.com/photos/70069/pexels-photo-70069.jpeg?cs=srgb&dl=pexels-daniyal-ghanavati-70069.jpg&fm=jpg", id: 1, title: "Bird image", tags: ["nature", "bird"] }, { link: "https://media.istockphoto.com/photos/renewable-energy-and-sustainable-development-picture-id1186330948?k=20&m=1186330948&s=612x612&w=0&h=5aNPCcQ8FcZraX44PEhb2mqcHkow2xMITJMHdh28xNg=", id: 2, title: "nature 1", tags: ["nature"] }, { link: "https://images.unsplash.com/photo-1538941214074-9b578e573632?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxleHBsb3JlLWZlZWR8Nnx8fGVufDB8fHx8&w=1000&q=80", id: 3, title: "river image", tags: ["nature", "river"] }, { link: "https://images.pexels.com/photos/459198/pexels-photo-459198.jpeg?cs=srgb&dl=pexels-pixabay-459198.jpg&fm=jpg", id: 4, title: "image river-nature", tags: ["nature", "river"] }, { link: "https://images.pexels.com/photos/1001682/pexels-photo-1001682.jpeg?cs=srgb&dl=pexels-kellie-churchman-1001682.jpg&fm=jpg", id: 5, title: "image12", tags: ["sea"] }, { link: "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7d/Wildlife_at_Maasai_Mara_%28Lion%29.jpg/800px-Wildlife_at_Maasai_Mara_%28Lion%29.jpg", id: 6, title: "Animal 1", tags: ["animal"] }, { link: "https://res.cloudinary.com/dk-find-out/image/upload/q_80,w_1920,f_auto/Animal_kingdom_nzwbda.jpg", id: 7, title: "Animal 2", tags: ["animal"] }, { link: "https://image.shutterstock.com/image-photo/side-view-lion-walking-panthera-260nw-165448391.jpg", id: 8, title: "lion 2", tags: ["lion"] }, { link: "https://images.pexels.com/photos/1172253/pexels-photo-1172253.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500", id: 9, title: "lifestyle image", tags: ["lifestyle", "river"] }, { link: "https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569__480.jpg", id: 10, title: "galaxy 1", tags: ["galaxy"] }, { link: "https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bGVuc3xlbnwwfHwwfHw%3D&w=1000&q=80", id: 11, title: "Lens", tags: ["lens"] }, { link: "https://cdn.pixabay.com/photo/2015/04/19/08/32/marguerite-729510__480.jpg", id: 12, title: "image flower", tags: ["nature", "flower"] } ]
const allTags = []
data.forEach(item => {
const {tags} = item
allTags.push(...tags)
})
const filtered = [...new Set(allTags)].sort()
console.log(filtered)

How to store a component in an object in typescript?

So I import multiple icons in react app and store them in an array of objects, it looks like below:
export const icons = [
{
title: 'Home',
link: 'home',
icon: <AiOutlineHome className="icon"/>,
},
{
title: 'Favorite',
link: 'favorite',
icon: <BsHeart className="icon"/>,
},
{
title: 'Search',
link: 'search',
icon: <IoSearchOutline className="icon"/>,
},
{
title: 'Playing',
link: 'playing',
icon: <MdOutlineMovieCreation className="icon"/>,
},
{
title: 'Upcoming',
link: 'upcoming',
icon: <BsCalendarDate className="icon"/>,
},
{
title: 'Popular',
link: 'popular',
icon: <HiOutlineFire className="icon"/>,
},
{
title: 'Popular',
link: 'popular',
icon: <AiOutlineStar className="icon"/>,
},
]
Then I import them in another component and map through them to display all icons. So when file is icons.js it works alright, but when I change it to icons.ts I get an error: 'AiOutlineHome' refers to a value, but is being used as a type here. Did you mean 'typeof AiOutlineHome'? with all icons. Is there some way to solve it or should I just stick to using javascript?
You need to name the extension to .tsx if you have JSX syntax in it.

react-i18next issue translating json values from translation file

I'm developing a multilanguage site and in several sections I use a hardcoded json to store some clients details (name, logo, project description and website) and then I map the json file and print the details using a component nothing fancy.
The problem is that I'm trying to set multilanguage and I don't know why when I try to set translated values to the json is not working. This is how I'm trying it now:
This is my react component:
import { useTranslation } from "react-i18next";
const { t } = useTranslation();
const companyADescription = t("projects.companyA.name");
{
name: "Company A",
url: "company.com",
image: "/assets/projects/a.png",
technologies: [
{
icon: "/assets/tech/react.png",
link: "#",
alt: "",
},
{
icon: "/assets/tech/firebase.png",
link: "#",
alt: "",
},
],
description: t("project.companyA.name"),
},
{
name: "Company B",
url: "company.com",
image: "/assets/projects/b.png",
technologies: [
{
icon: "/assets/tech/react.png",
link: "#",
alt: "",
},
{
icon: "/assets/tech/firebase.png",
link: "#",
alt: "",
},
],
description: t("projects.companyB.name"),
},
And this is my json file with the translation:
"projects": {
"companyA": {
"name": "Company A Project",
"description": "This is the company project description A"
}
}
On other sections I use the t function from the useTranslation hook and works well I already spent a lot of time on this issue :(
The problem is here
description: t("project.companyA.name")
The function isnt being called
You can try this above declaration of JSON
const descriptionA = t("project.companyA.name")
const descriptionB = t("projects.companyB.name")
And then assign the values in JSON as
{
name: "Company A",
url: "company.com",
image: "/assets/projects/a.png",
technologies: [
{
icon: "/assets/tech/react.png",
link: "#",
alt: "",
},
{
icon: "/assets/tech/firebase.png",
link: "#",
alt: "",
},
],
description: descriptionA ,
},
{
name: "Company B",
url: "company.com",
image: "/assets/projects/b.png",
technologies: [
{
icon: "/assets/tech/react.png",
link: "#",
alt: "",
},
{
icon: "/assets/tech/firebase.png",
link: "#",
alt: "",
},
],
description: descriptionB ,
},

How to set json data to footer list react

I start working with react and i need to set mock json data to list in footer.
How i can set list dynamically to each module. I have 3 modules Company, Catalog, Contacts, and JSON:
const FOOTER_DATA = {
menu: {
title: "Company",
items: [
{
title: "Link 1",
link: "#",
},
{
title: "Link 2",
link: "#",
}
],
},
catalog: {
title: "Catalog",
items: [
{
title: "Link Link 1",
link: "#",
},
{
title: "LinkLink 2",
link: "#",
},
{
title: "LinkLink 3",
link: "#",
},
{
title: "LinkLink 4",
link: "#",
},
{
title: "LinkLink 5",
link: "#",
},
{
title: "LinkLink 6",
link: "#",
}
],
},
contact: {
email: "some-mail#gmail.com",
tel: "777 777 777",
work_time: {
weekdays: "09:00 - 18:00",
weekends: "10:00 - 16:00",
},
},
};
How i can set this for each jsx file, to special tags:
<Company />
<Categories />
<Contacts />
I am really beginner in react an i stuck in such issues.
You should pass the data as a prop to each element and render the data inside each component.
<Company data={FOOTER_DATA?.menu}/>
<Categories data={FOOTER_DATA?.catalog}/>
<Contacts data={FOOTER_DATA?.contact}/>
For example you may have a JSX component for Company like below,
import React from "react";
const Company = ({ data }) => {
return (
<React.Fragment>
<h2>{data.title}</h2>
{data.items.map((item, index) => (
<a key={index} href={item.link}>
{item.title}
</a>
))}
</React.Fragment>
);
};
export default Company;

Resources