渲染多段文本的最佳方法从JSON读入React组件

时间:2016-06-13 06:41:01

标签: javascript json reactjs

我不知道最好的方法。

给定具有硬编码文本的React组件:

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>
        )
    }
})

现在你可以看到我正在通过{company.interview.testTypes.questions[0].questionSubDescription}

从json文件中读取问题

现在我想把它下面的两个分段的答案移到json中,我想保留json中的段落。意思是我希望能够将带有内容的p标签移动到json“answer”字段中,当反应呈现时,它就像上面一样呈现,内容周围带有

标签。

这是 types.json 部分

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

我想将该内容放入答案字段。换句话说,我想说明一下:

<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>

进入我的json对象中的"answer"字段。

然后我将在我的React组件中替换它,并从json文件中读取答案:

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>
        )
    }
})

1 个答案:

答案 0 :(得分:0)

您可以将文本放在数组

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

然后在组件中使用它

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>;
  }
})