[英]React: How to display JSON key value pairs with line breaks?
I have the following component with a function called onClickBtn()
that contains another function called textBuilder()
which iterated through a JSON object filled with data from a form. 我有一个带有名为
onClickBtn()
的函数的以下组件,该组件包含另一个名为textBuilder()
函数,该函数通过填充有来自表单数据的JSON对象进行迭代。 I am using an email service to programatically send an email when the submit button is clicked. 单击提交按钮时,我正在使用电子邮件服务以编程方式发送电子邮件。 The only issue is that my email's
Body
value ends up looking like this in the email: 唯一的问题是我的电子邮件的“
Body
价值”最终在电子邮件中看起来像这样:
name: Dean age: 26 email: dean@aol.com styles: Color locations: Right Leg specificLocation: fasdfasdfa description: fdghjdghj budgets: $1000-$1500 days: Wednesday months: June
Instead of: 代替:
name: Dean
age: 26
email: dean@aol.com
styles: Color
locations: Right Leg
specificLocation: fasdfasdfa
description: fdghjdghj
budgets: $1000-$1500
days: Wednesday
months: June
Below is my component. 以下是我的组件。 Can someone tell me what I am doing wrong?
有人可以告诉我我在做什么错吗?
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
const useStyles = makeStyles(theme => ({
button: { margin: theme.spacing(1), },
input: { display: 'none', },
}));
export default function ContainedButtons(props) {
const classes = useStyles();
const onClickBtn = () => {
const textBuilder = () => {
var doc = "";
for (const key in props.values) {
doc += key + ": " + props.values[key] + "\n";
}
return doc;
}
window.Email.send({
Host : "smtp.elasticemail.com",
Username : "dfmmalaw@gmail.com",
Password : "d2296492-2689-49f7-ae3d-584f1507d23a",
To : 'dfmmalaw@gmail.com',
From : "dfmmalaw@gmail.com",
Subject : "This is the subject",
Body : textBuilder()
}).then(
message => alert(message)
);
};
return (
<div>
<Button variant="contained" color="primary" className={classes.button} disabled = {!props.isEnabled} type="submit" onClick={onClickBtn}>
Submit
</Button>
</div>
);
}
It's because your email client is rendering the content as HTML. 这是因为您的电子邮件客户端将内容呈现为HTML。 So newlines are just converted to spaces.
因此,换行符只是转换为空格。 Try it with a break element, like so:
尝试使用break元素,如下所示:
doc += key + ": " + props.values[key] + "<br/>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.