无法在React中使用FontAwesome在'Node'上执行'removeChild'

Dav*_*vid 17 font-awesome reactjs

每当我尝试className='fa-spin'在React中使用FontAwesome微调器图标(带)时,我收到以下错误:

Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
at removeChild (http://localhost:5000/public/bundle.js:19553:22)
at unmountHostComponents (http://localhost:5000/public/bundle.js:13683:11)
at commitDeletion (http://localhost:5000/public/bundle.js:13727:5)
at commitAllHostEffects (http://localhost:5000/public/bundle.js:14419:13)
at HTMLUnknownElement.callCallback (http://localhost:5000/public/bundle.js:5035:14)
at Object.invokeGuardedCallbackDev (http://localhost:5000/public/bundle.js:5074:16)
at invokeGuardedCallback (http://localhost:5000/public/bundle.js:4931:27)
at commitRoot (http://localhost:5000/public/bundle.js:14508:9)
at performWorkOnRoot (http://localhost:5000/public/bundle.js:15510:42)
at performWork (http://localhost:5000/public/bundle.js:15460:7)
Run Code Online (Sandbox Code Playgroud)

编辑:现在问题出现了几次,代码本身并没有什么特别之处.我一直在使用微调器作为加载图标,每当微调器被内容替换时就会发生错误.例:

return (
  <div>
    {this.state.loading === true ? <i className="fa-spin fas fa-sync"></i> : (
      this.state.recipes.length === 0 ? (
        <div className='text-center'>
          <h2>There doesn't seem to be anything here...</h2><br />
          <h2 style={buttonStyle}>Get started by </h2><button style={buttonStyle} className='btn btn-md btn-success' onClick={(e) => this.props.setView(e, 'browserecipes')}>browsing existing recipes</button><h2 style={buttonStyle}> or </h2><button style={buttonStyle} className='btn btn-success btn-md' onClick={(e) => this.props.setView(e, 'addrecipe')}>adding a recipe.</button>
        </div>
      ) : (
      <div>
          <h1 className='text-center title'>My Recipe Cloud</h1>
          <RecipeContainer
            recipes={this.state.recipes}
            user={this.state.user}
            tags={this.props.tags}
            setView={this.props.setView}
            changeUser={this.changeUser}
          />
        </div>
      )
    )}
  </div>
Run Code Online (Sandbox Code Playgroud)

)

Sir*_*man 40

我想我弄清楚为什么会这样.它似乎与FontAwesome 5用<i>标签替换标签的方式有关<svg>.我相信这与React处理从DOM中删除元素的方式不兼容.请参阅:https://fontawesome.com/how-to-use/svg-with-js#with-jquery

我使用的解决方法在该文档的底部注明,其中包括:

<script>
  FontAwesomeConfig = { autoReplaceSvg: 'nest' }
</script>
Run Code Online (Sandbox Code Playgroud)

我将它包含在我的标题中,可能有更好的位置,但它似乎至少为我解决了问题.它可能会影响你可能对任何你专门针对FontAwesome元素的类所拥有的CSS逻辑,这些元素是其他类/ ids的直接子元素,因此你可能只想检查以确保所有样式看起来都正确,因为它现在正在嵌套在<svg>该范围内标签<i>的标签,而不是取代它.

或者你可以<i>自己包装标签.例如:

{this.state.loading === true ? <div><i className="fa-spin fas fa-sync"></i></div> ...
Run Code Online (Sandbox Code Playgroud)

应该管用.

UPDATE(18年12月10日):现在有为什么这是在官方的文档发生的文档中更好的解释以及如何将这个FontAwesome用JavaScript库集成了一个解释这里.<i>现在,在脚本标记内部完成了自动嵌套标记的方法,用于获取FontAwesome javascript库<script src="https://use.fontawesome.com/releases/v5.5.0/js/all.js" data-auto-replace-svg="nest"></script>.现在还有对FontAwesome React库的官方支持,该也解决了这个问题.