操纵< html>或< body>在React中标记 [英] Manipulate the <html> or <body> tag in React

查看:94
本文介绍了操纵< html>或< body>在React中标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在React中操纵< html> < body> 标签的最佳方法是什么?

What is the best way to manipulate the <html> or <body> tags in React?

例如,动态设置 lang属性或更改类别?

For example, dynamically setting 'lang' attribute or change classes?

<html lang="en" class="ltr">

我可以使用原始DOM操作来做到这一点。但这是正确的方法吗?

I could do that using raw DOM manipulation. But is this the right way?

推荐答案

我刚刚遇到了此用例,我想在其中修改<$ c $当用户切换语言时,c>< html> lang属性。

反应头盔实际上使这一过程非常简单。

I've just encountered this use case where I want to modify the <html> lang attribute when language is switched by the user.
React Helmet made this quite simple actually.

只需从反应状态获取lang并将其传递到应用程序中任意位置的Helmet组件:

Just get the lang from your react state and pass it to the Helmet component anywhere in your app:

<Helmet htmlAttributes={{ lang : this.state.lang }}/> // with this.state = { lang : 'en' }

这篇关于操纵&lt; html&gt;或&lt; body&gt;在React中标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆