How to update meta tags in React.js?

后端 未结 9 937
清酒与你
清酒与你 2020-12-28 12:05

I was working on a single page application in react.js, so what is the best way to update meta tags on page transitions or browser back/forward?

9条回答
  •  渐次进展
    2020-12-28 12:33

    You can use the document query and update their values.

    const setTitle = title => {
        const el = document.querySelector('title');
        el.innerText = `${el.text} | ${title}`;
      };
    
    const setDescription = desc => {
        const el = document.querySelector("meta[name='description']");
        el.setAttribute('content',desc)
      }
    

提交回复
热议问题