在 React 的模板文字中插入 html 标签
Inserting html tags in template literals in React
我在 React 中有带有模板字面量的变量:
const weatherForecast = `
Today in <strong>${this.state.cityName}</strong>
weather will be with <strong>${today.weather_state_name}</strong>,
min temperature <strong>${parseInt(today.min_temp)}°C</strong>,
max temperature <strong>${parseInt(today.max_temp)}°C</strong>,
and humidity will be <strong>${today.humidity}%</strong>
`;
我想在其中插入 HTML 代码,如您所见,那是 tag 。这可能吗,我该怎么做。我用谷歌搜索,但找不到答案。
您需要解析 HTML,因为在您的情况下,它会显示您的 标签,而不是使文本变强。
你可以使用例如import ReactHtmlParser from 'react-html-parser';
ReactHtmlParser(weatherForecast)
它回答了你的问题吗?
const weatherForecast =
`Today in <strong>${this.state.cityName}</strong> weather will be with <strong>${today.weather_state_name}</strong>, min temperature <strong>${parseInt(today.min_temp)}°C</strong>, max temperature <strong>${parseInt(today.max_temp)}°C</strong>, and humidity will be <strong>${today.humidity}%</strong>`;
React 会将其视为字符串而不是 jsx。
你可以做的是
const weatherForecast =
<p>Today in <strong>{this.state.cityName}</strong> weather will be with <strong>{today.weather_state_name}</strong>, min temperature <strong>{parseInt(today.min_temp)}°C</strong>, max temperature <strong>{parseInt(today.max_temp)}°C</strong>, and humidity will be <strong>{today.humidity}%</strong></p>;
然后在你的 render
方法中渲染它,像这样
render(){
return <div>{weatherForecast}</div>
}
您可以使用 dangerouslySetInnerHTML
通过 React 从字符串中呈现 HTML,但请确保仅在绝对必要且您控制值时才使用它。
例子
class App extends React.Component {
state = {
cityName: "New York",
today: {
weather_state_name: "Foo",
min_temp: 0,
max_temp: 10,
humidity: 50
}
};
render() {
const { today } = this.state;
const weatherForecast = `
Today in <strong>${this.state.cityName}</strong>
weather will be with <strong>${today.weather_state_name}</strong>,
min temperature <strong>${parseInt(today.min_temp)}°C</strong>,
max temperature <strong>${parseInt(today.max_temp)}°C</strong>,
and humidity will be <strong>${today.humidity}%</strong>
`;
return <div dangerouslySetInnerHTML={{ __html: weatherForecast }} />;
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
<div dangerouslySetInnerHTML={{__html: weatherForecast }} />
您可以使用此代码显示。
用 JSX 编写:
const weatherForecast = <div>
Today in <strong>{this.state.cityName}</strong>
weather will be with <strong>{today.weather_state_name}</strong>,
min temperature <strong>{parseInt(today.min_temp)}°C</strong>,
max temperature <strong>{parseInt(today.max_temp)}°C</strong>,
and humidity will be <strong>{today.humidity}%</strong>
</div>;
记得省略美元符号 ($)
我在 React 中有带有模板字面量的变量:
const weatherForecast = `
Today in <strong>${this.state.cityName}</strong>
weather will be with <strong>${today.weather_state_name}</strong>,
min temperature <strong>${parseInt(today.min_temp)}°C</strong>,
max temperature <strong>${parseInt(today.max_temp)}°C</strong>,
and humidity will be <strong>${today.humidity}%</strong>
`;
我想在其中插入 HTML 代码,如您所见,那是 tag 。这可能吗,我该怎么做。我用谷歌搜索,但找不到答案。
您需要解析 HTML,因为在您的情况下,它会显示您的 标签,而不是使文本变强。
你可以使用例如import ReactHtmlParser from 'react-html-parser';
ReactHtmlParser(weatherForecast)
它回答了你的问题吗?
const weatherForecast =
`Today in <strong>${this.state.cityName}</strong> weather will be with <strong>${today.weather_state_name}</strong>, min temperature <strong>${parseInt(today.min_temp)}°C</strong>, max temperature <strong>${parseInt(today.max_temp)}°C</strong>, and humidity will be <strong>${today.humidity}%</strong>`;
React 会将其视为字符串而不是 jsx。 你可以做的是
const weatherForecast =
<p>Today in <strong>{this.state.cityName}</strong> weather will be with <strong>{today.weather_state_name}</strong>, min temperature <strong>{parseInt(today.min_temp)}°C</strong>, max temperature <strong>{parseInt(today.max_temp)}°C</strong>, and humidity will be <strong>{today.humidity}%</strong></p>;
然后在你的 render
方法中渲染它,像这样
render(){
return <div>{weatherForecast}</div>
}
您可以使用 dangerouslySetInnerHTML
通过 React 从字符串中呈现 HTML,但请确保仅在绝对必要且您控制值时才使用它。
例子
class App extends React.Component {
state = {
cityName: "New York",
today: {
weather_state_name: "Foo",
min_temp: 0,
max_temp: 10,
humidity: 50
}
};
render() {
const { today } = this.state;
const weatherForecast = `
Today in <strong>${this.state.cityName}</strong>
weather will be with <strong>${today.weather_state_name}</strong>,
min temperature <strong>${parseInt(today.min_temp)}°C</strong>,
max temperature <strong>${parseInt(today.max_temp)}°C</strong>,
and humidity will be <strong>${today.humidity}%</strong>
`;
return <div dangerouslySetInnerHTML={{ __html: weatherForecast }} />;
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
<div dangerouslySetInnerHTML={{__html: weatherForecast }} />
您可以使用此代码显示。
用 JSX 编写:
const weatherForecast = <div>
Today in <strong>{this.state.cityName}</strong>
weather will be with <strong>{today.weather_state_name}</strong>,
min temperature <strong>{parseInt(today.min_temp)}°C</strong>,
max temperature <strong>{parseInt(today.max_temp)}°C</strong>,
and humidity will be <strong>{today.humidity}%</strong>
</div>;
记得省略美元符号 ($)