简体   繁体   English

反应,HTML 和 JavaScript:TypeError:无法设置 null 的属性“innerHTML”

[英]React, HTML and JavaScript: TypeError: Cannot set property 'innerHTML' of null

I am using React and I am getting the following error: TypeError: Cannot set property 'innerHTML' of null .我正在使用 React,我收到以下错误: TypeError: Cannot set property 'innerHTML' of null I did see some questions related to this error but I did not find any that helped me.我确实看到了一些与此错误相关的问题,但我没有找到任何对我有帮助的问题。

//React
class Pagination extends Component {

  pagincationScript = (totalPages, page) =>{
    const ulTag = document.querySelector("ul");

    let liTag = '';
    if(page > 1){
      liTag = `<li className="Btn Previous"><span><i>&#8592;</i>Previous</span></li>`
    }

ulTag.innerHTML = liTag;

  }

  render() {

    return (
      <div className="Pagination">
        <ul>

        {this.pagincationScript(10, 5)}

        </ul>
      </div>
    )
  }
}

export default Pagination;

When writing React you should always be writing JSX, like in the render function you wrote.在编写 React 时,您应该始终编写 JSX,就像在您编写的render function 中一样。 To render the <li> separately you can do something like the following:要单独渲染<li>您可以执行以下操作:

function Li() {
  return (
    <li className="Btn Previous"><span><i>&#8592;</i>Previous</span></li>
  )
}

export default function Pagination() {
  // Loop as many times as needed
  const lis = [];
  for (let i = 0; i < 10; i++) {
    lis.push(<Li key={i} />);
  }

  return (
    <div className="Pagination">
      <ul>
        {lis}
      </ul>
    </div>
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用JavaScript创建HTML? 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - How to create html with JavaScript? Uncaught TypeError: Cannot set property 'innerHTML' of null 未被捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null:javascript - Uncaught TypeError: Cannot set property 'innerHTML' of null: javascript 反应:TypeError:无法读取 null 的属性“innerHTML” - React: TypeError: Cannot read property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的Typeerror:无法将属性innerHTML设置为null - Uncaught Typeerror: Cannot set property innerHTML of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 类型错误:无法在 reactJS 中设置 null 的属性“innerHTML” - TypeError: Cannot set property 'innerHTML' of null in reactJS getElementById TypeError:“无法将属性&#39;innerHTML&#39;设置为null - getElementById TypeError: "cannot set property 'innerHTML' of null 类型错误:无法设置 null 的属性“innerHTML” - TypeError: Cannot set property 'innerHTML' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM