Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

javascript - 将.then()内部的字符串解析为JSX元素到外部(get parse String inside .then() to outside as JSX element)

In react I import these following libraries

(在反应中,我导入以下库)

import React from "react";
import {
  Container,
  Row,
  Col,
  Card,
  CardBody,
  CardFooter,
  Badge,
  Button
} from "shards-react";

inside the functions, I have the following fetch method and inside .then() I add JSX elements

(在函数中,我有以下提取方法,在.then()中,我添加了JSX元素)

     output=''
fetch(
          "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@MediumStaff"
        )
          .then(res => res.json())
          .then(data => {
            const res = data.items; 

            let a = 0;
            res.forEach(item => {

              if (a === 0 || a === 3 || a === 6 || a === 9) {
                output += `<Row>`;
              }

              output += `<Col lg="6" md="6" sm="12" className="mb-4">
                <Card small className="card-post card-post--1">
                  <div
                    className="card-post__image"
                    style={{ backgroundImage: "url("+"${item.thumbnail}"+")" }}
                  >
                  </div>
                  <CardBody>
                  //JSX elements
                  </CardBody>
                </Card>
                </Col>`;
              if (a === 2 || a === 5 || a === 8 || a === 9) {
                output += `</Row>`;
              }

              a += 1;
            });

            document.querySelector(".blog__slider").innerHTML = output;
            console.log(output);

          });

In return I used the following code

(作为回报,我使用了以下代码)

<div className="blog__slider"></div>

I thought the functional component in React did not take output values as JSX components.

(我认为React中的功能组件不像JSX组件那样接受输出值。)

If I get update output value outside of .then() I could use dangerouslySetInnerHTML={{__html: output}}

(如果我在.then()之外获取更新输出值.then()可以使用dangerouslySetInnerHTML={{__html: output}})

  ask by Sathananthan Sabesan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...