Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Render Multiple Paragraph Text read from JSON into React Component

I don't know of the best way to do this.

Given a React Component that has hard coded text:

const TestTypes = Component({
    store: Store('/companies'),
    render(){
        var company = this.store.value()[this.props.companyId];

        return (
            <div id='ft-test-types className="all-100"'>
                <p className="section-heading bold padding-top-20 font-22">Types of Tests</p>
                <div className="all-100 padding-left-30 align-left">
                    <div className="all-100 align-left">
                        <p className="bold blue margin-2">{company.interview.testTypes.questions[0].question}</p>

                        <p className="italic padding-left-30 padding-top-20">
                            bunch of text for this paragraph...
                        </p>  
                        <p className="italic padding-left-30">
                            More text in this paragraph
                        </p>

                    </div>
                </div>
            </div>
        )
    }
})

So right now you can see I'm reading the question from a json file via {company.interview.testTypes.questions[0].questionSubDescription}

Now I want to move the answer which are the two pagragraphs below it into json and I want to keep the paragraphs in tact from json. Meaning I want to be able to move those p tags with the content into a json "answer" field and when react renders, it renders just like it does above, with

tags around the content.

Here's part of the types.json:

[{
    "testTypes": {
        "questions": [{
            "question": "Can you explain the different types and layers (scopes & boundaries) of tests",
            "answer": ""
        }]
    }
}]

I want to put that content into the answer field. In other words I want to put this:

<p className="italic padding-left-30 padding-top-20">
    bunch of text for this paragraph...
</p>  
<p className="italic padding-left-30">
    More text in this paragraph
</p>

Into the "answer" field in my json object.

Then I'll replace it in my React component as so and read from the json file for the answer:

const TestTypes = Component({
    store: Store('/companies'),
    render(){
        var company = this.store.value()[this.props.companyId];

        return (
            <div id='ft-test-types className="all-100"'>
                <p className="section-heading bold padding-top-20 font-22">Types of Tests</p>
                <div className="all-100 padding-left-30 align-left">
                    <div className="all-100 align-left">
                        <p className="bold blue margin-2">
                            {company.interview.testTypes.questions[0].question}
                        </p>
                        {company.interview.testTypes.questions[0].answer}
                    </div>
                </div>
            </div>
        )
    }
})
like image 943
PositiveGuy Avatar asked Jan 21 '26 19:01

PositiveGuy


1 Answers

You can put your text inside array

"answer": [
  'bunch of text for this paragraph...',
  'More text in this paragraph'
]

and then use it inside your component

const TestTypes = Component({
  store: Store('/companies'),
  render(){
    var company = this.store.value()[this.props.companyId];
    var answer = company.interview.testTypes.questions[0].answer.map(function (text, index) {
      const paddingTopClass = index === 0 ? 'padding-top-20' : '';

      return <p
        key={ index }
        className={ `italic padding-left-30 ${ paddingTopClass }` }
      >
        { text }
      </p>
    });

    return <div id='ft-test-types className="all-100"'>
      <p className="section-heading bold padding-top-20 font-22">Types of Tests</p>
      <div className="all-100 padding-left-30 align-left">
        <div className="all-100 align-left">
          <p className="bold blue margin-2">
            {company.interview.testTypes.questions[0].question}
          </p>

          { answer }
        </div>
      </div>
    </div>;
  }
})
like image 182
Oleksandr T. Avatar answered Jan 24 '26 10:01

Oleksandr T.