[英]React component, or strategy to replace string literals from stored data similar to react-intl
I am receiving data from an api call similar to this. 我正在从类似于此的api调用接收数据。
simplified_response: {
variables:{
name: 'John',
age: '25',
gender: 'male',
},
strings:[
'<p>Hello ${name}</p>',
'<p>${name},<br/> I understand you are ${age} years old</p>',
'<p>${name},<br/> I understand you are ${gender}</p>',
},
};
I am trying to replace the string literals before displaying in react. 我正在尝试在响应中显示之前替换字符串文字。 Is there a simplistic way to do this similar to react-intl FormattedMessage?
有没有一种简单的方法可以做到这一点,就像react-intl FormattedMessage一样?
In react-intl I think you could do something like this if the $
weren't present 在react-intl中,我认为如果不存在
$
,您可以执行以下操作
import React from 'react';
import R from 'ramda';
import { FormattedHTMLMessage } from 'react-intl';
export const print = (props) => {
return (
<div>
{ R.map((string) => (
<FormattedHTMLMessage
defaultMessage={ string }
values={ props.response.variables }
/>
), props.response.strings) }
</div>
);
};
Expandable.propTypes = {
response: PropTypes.obj,
};
Try this: 尝试这个:
let { variables, strings } = simplified_response
for (let [key, value] of Object.entries(variables)) {
strings = strings.map(string => string.replace('${' + key + '}', value))
}
simplified_response.strings = strings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.