0

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?

9
  • so easy... { Text[p6_q${String(obj)}] } (there are back ticks after the open bracket and before the close bracket) Commented Mar 29, 2019 at 2:08
  • @JoaozitoPolo It shows me error that requires ']' after p6_q Commented Mar 29, 2019 at 2:17
  • what is the result of {console.log(obj)} Commented Mar 29, 2019 at 2:20
  • @JSEngine it shows 1,2,3,4,5,6,7,8. the original List which call this Item component is [1,2,3,4,5,6,7,8] Commented Mar 29, 2019 at 2:23
  • could you again use { Text[`p6_q${String(obj)}`] } Commented Mar 29, 2019 at 2:29

1 Answer 1

1

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])}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.