How can I insert Variable into variable name which is imported from other file?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I want to put variables(in loop) to call const name, which I imported from other file.

I want to import const from file like this:

export const p6_q1="AAA";
export const p6_q2="BBB";
export const p6_q3="CCC";
export const p6_q4="DDD";

I've tried this :

import React from 'react';
import * as Text from 'textKorean';

const FaqItem = ({obj}) => {
    return (
        <div>
            {console.log(obj)}
            { Text.p6_q`${String(obj)}` }
        div>
    );
};

export default FaqItem;

props 'obj' is well presented on my console. But the error message says:

./src/FaqItem.js Attempted import error: 'p6_q' is not exported from 'textKorean' (imported as 'Text').

I want it to be p6_q1. p6_q2, p6_q3, ... and so on.

but my code in { Text.p6_q`${String(obj)}` } says it is just p6_q.

How can I do that?

profilepic.png
manpreet 2 years ago

First of all, you should probably change the import to import * as Text from './textKorean.js', if the textKorean file is in the same folder as your FaqItem. Next, is your obj is an array, you'll have to map the array. You should try

{ obj.map(num => Text["p6_q"+num])}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.