4

How can I add an attribute to html element in React js. I want to set <html lang="en"> to solve WCAG Level A validation issue.

I added html-element-attributes module but no idea about how to use it.

Anybody please help to get a way to add this lang attribute to html element?

6
  • 2
    It's kinda unusual for React to control the entire document, it's normally rendered into an element on an already existing page in which case that would be set in the template for the page and not by React. Can you elaborate on what you're trying to do? Commented Aug 4, 2017 at 6:14
  • WCAG validation is showing an error to add lang attribute for html. SoI am trying to add this. Commented Aug 4, 2017 at 6:18
  • Yes, I understand that but are you sure your <html> element is managed by React? Without knowing what your setup is it's very difficult to know how to fix this. Commented Aug 4, 2017 at 6:23
  • why don't put it in index.html file? Commented Aug 4, 2017 at 6:38
  • 2
    There is no index.html file in React Commented Aug 4, 2017 at 6:40

2 Answers 2

5

you need React helmet for this. Put this block in any top level component's render:

<Helmet>
  <html lang="en" />
</Helmet>
Sign up to request clarification or add additional context in comments.

Comments

0

I added the lang property directly by creating a div surrounding the code on my app.js file.

  return (
    <Provider store={store}>
      <CacheProvider>
        <div lang='en'>
          <Head>
            ...
          </Head>
          <SidebarProvider>
            <ThemeProvider>
              <LocalizationProvider dateAdapter={AdapterDateFns}>
                <CssBaseline />
                {getLayout(<Component {...pageProps} />)}
              </LocalizationProvider>
            </ThemeProvider>
          </SidebarProvider>
        </div>
      </CacheProvider>
    </Provider>
  );

I am sharing the complete code in order to give the context. The only difference I made for this purpose was to add the div.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.