Escapehtml phản ứng

It says clearly that by default React DOM escapes any values embedded in JSX unless it is explicitly written in code. So what that basically mean is if we try to render user input(this could be response data) it will be escaped(an escape is a fancy word for remove or replace). But as i understood this will not escape elements such as new lines(new line is technically not a tag which brings us to another topic
vs \n). But the new line will not take affect unless we specify it with CSS.

// 1: Note - This will NOT create a new line since it is escaped and // tag 

will be just output as a string// response data string could look something like this
// '

First line\nSecond line

'const myString = responseDataString{myString}// output looks something like

First line Second line


// i know it's weird but there will be a space replacing \n but
// that't not what we wanted right?

Nhưng bạn có thể xử lý các loại ký tự dòng mới này bằng cách sử dụng thuộc tính css trên phần tử nhắm mục tiêu

white-space: pre-line;
// pre-wrap also could be used where it will preserve all the
// multiple white spaces where pre-line will remove excess but both
// will break into new lines when a new line is present.

There may be instances where we may need to set HTML strings in React. An example would be your app receives a string in a response where the data is a HTML formatted string and we want to render it according to that structure.(this could be some CMS sending an input of a text area where

, ,

  • etc tags could exist). In such cases we cannot just render it in react as a string. Technically we could but it will be shown just as a string. If we want to render it as the pure HTML objects we need to use the dangerosulySetInnerHTML prop in react. This hideous name is given because this has an underlying serious issue. We could receive the HTML formatted string that could harm the app.For an example if you receive something like this as the HTML string it will show an alert which is not fun anymore.

    "
    Escapehtml phản ứng

    1. Tôi nhận được thông tin sau trên trang chỉnh sửa

    Escapehtml phản ứng

    Đây là thành phần của tôi

    import React, { Component } from "react";
    import { withRouter } from "react-router-dom";
    import "react-quill/dist/quill.snow.css";
    import ReactQuill from "react-quill";
    import swal from "sweetalert";
    import axios from "axios";
    
    import AuthService from "../../Auth/AuthService";
    import withAuth from "../../Auth/withAuth";
    const Auth = new AuthService();
    
    class Edit extends Component {
      constructor(props) {
        super(props);
        this.state = {
          journal: {}
        };
      }
    
      componentDidMount() {
        const journalId = this.props.match.params.id;
        axios
          .get("/api/journals/" + journalId)
          .then(result => {
            this.setState({ journal: result.data });
          })
          .catch(error => {
            console.error(error);
          });
      }
    
      modules = {
        toolbar: [
          [{ header: [1, 2, false] }],
          ["bold", "italic", "underline", "strike", "blockquote"],
          [
            { list: "ordered" },
            { list: "bullet" },
            { indent: "-1" },
            { indent: "+1" }
          ],
          ["link", "image"],
          ["clean"]
        ]
      };
    
      formats = [
        "header",
        "bold",
        "italic",
        "underline",
        "strike",
        "blockquote",
        "list",
        "bullet",
        "indent",
        "link",
        "image"
      ];
    
      onChange = e => {
        const state = this.state.journal;
        state[e.target.name] = e.target.value;
        this.setState({ journal: state });
      };
    
      handleQuillChange = value => {
        this.setState({ content: value });
      };
    
      onSubmit = e => {
        e.preventDefault();
        const journalId = this.props.match.params.id;
        const { title, content } = this.state.journal;
    
        let config = {
          headers: { Authorization: "bearer " + Auth.getToken() }
        };
    
        let body = {
          title,
          content
        };
    
        axios
          .put("/api/journals/" + journalId, body, config)
          .then(result => {
            swal({
              title: "Success",
              text: `You have edited the journal ${title}`,
              icon: "success",
              button: "OK"
            });
            this.props.history.push("/journals/" + journalId);
          })
          .catch(error => {
            swal({
              title: "Error",
              text: `${error}`,
              icon: "error",
              button: "Try again"
            });
          });
      };
    
      render() {
        return (
          <>
            

    Edit a journal

    Title:

    content:

    Submit

    ); } } export default withAuth(withRouter(Edit));

    Vì vậy, bất kỳ trợ giúp nào cũng sẽ tuyệt vời khi giải quyết cả hai vấn đề

    React có tự động thoát khỏi HTML không?

    React xuất các phần tử và dữ liệu bên trong chúng bằng cách sử dụng thoát tự động . Nó diễn giải mọi thứ bên trong validationMessage dưới dạng một chuỗi và không hiển thị bất kỳ phần tử HTML bổ sung nào.

    Nguy hiểmSetInnerHTML trong React là gì?

    Phản ứng nguy hiểmSetInnerHTML là một thuộc tính HTML giúp dễ dàng lập trình các phần tử HTML từ một nguồn bên ngoài . Nó đã thay thế InternalHTML được sử dụng trong DOM của trình duyệt. nguy hiểmSetInnerHTML giúp React biết cách xử lý các phần tử HTML trong một thành phần nơi nội dung sẽ được hiển thị.

    Làm cách nào để sử dụng Unicode trong JSX?

    Có thể thêm Unicode vào React. .
    Chúng tôi chuyển %C9ire bên trong tên prop. .
    Bên trong chức năng kết xuất của chúng tôi, {this. đạo cụ. name} hiển thị thành công chuỗi của chúng tôi và các ký tự Unicode của nó