在JSX中显示html实体上标

时间:2019-05-21 21:05:32

标签: javascript reactjs jsx

新的反应。我正在获取具有此值的JSON

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from apiclient.http import MediaFileUpload

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
service = None

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    global service
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    results = service.files().list(
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

main()

# Retrieve the parent ID of the files/ directory
dirp = "files" # Name of directory to find.
parent_id = "" # The id we are looking for.
query = ("name='%s'" % (dirp))
resp = service.files().list(
    q=query,
    fields="files(id, name)",
    pageToken=None).execute()
files = resp.get('files', [])

# Create a file object for file 'report.csv' on your local drive.
media = MediaFileUpload('report.csv',
                        mimetype='text/csv',
                        resumable=True)

# Upload the file.
if len(files) > 0:
    parent_id = files[0].get('id')
    meta_data= { 'name': 'report.csv',
                 'parents': [parent_id],
                 'mimeType' : 'application/vnd.google-apps.spreadsheet' }
    f = service.files().create(
        body=meta_data,
        media_body=media,
        fields='id').execute()
    if not f is None: print("[*] uploaded %s" % (f.get('id')))
else: print("The folder files/ does not exist on your drive.")

我将其设置为状态变量,然后使用其显示在标题中。 我之前在Django中完成了此操作,并将其正确转换,但我看不到这种情况。我发送数据错误还是有办法做到?

{
"unit": "G/ft²/yr"
}
setUnit({
        water_flow_unit: item.unit
      });

它显示为<h4 className="baseline-graph-header"> Unit({Unit.unit}) </h4> ,但我希望将其转换。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我发现最适合我的解决方案是

<span
 dangerouslySetInnerHTML={{
  __html: `${Unit.unit})`
   }}
 />

确保在span或div元素中将其删除,否则会得到- 对象作为React子对象无效(找到:带有键{__html}的对象)。如果要渲染子级集合,请改用数组-错误

引用:https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml