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 // tagwill 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 likeFirst 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
, ,
" {
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 đề