代码之家  ›  专栏  ›  技术社区  ›  TheBlackBenzKid

如何在无状态条件外部反应组件中返回pass和return变量?

  •  1
  • TheBlackBenzKid  · 技术社区  · 7 年前

    我想向一个无状态函数发射一些变量,并将它们返回到现有的基于类的代码中。

    这是我的 Home 组件。

    import React, { Component } from 'react';
    import External from '/External';
    
    class Home extends Component {
    
        componentDidMount() {
            External(true);
        }
    
        componentWillUnmount() {
          External(false);
        }
    
        render(){
          return (
            <div className="homePage pageWrapper">
                Hello
            </div>
          )
        }
    
    }
    
    
    export default Home;
    

    这是我的外部组件,将在许多页面上使用。我希望重用它的功能。

    const External = ({}) => {
        if(true){
            return console.log('yes');
            // do something to the DOM
        } else {
            return console.log('no');
        }
    };
    

    我试着做到了 this.External() 我试过了 External('true') 传递文本,但这也不起作用。控制台只发出警告

    Line 2: 'External' is assigned a value but never used no-unused-vars

    1 回复  |  直到 7 年前
        1
  •  2
  •   Estus Flask    7 年前

    no-unused-vars ESLint警告表示代码存在实际问题。 External

    它应该是默认导出:

    export ({}) => ...
    

    import External from '/External';
    

    export const External = ({}) => ...
    

    import { External } from '/External';