Reactjs “反应”;这还没有';尚未初始化-super()尚未';我不曾被称为;仅限内置

Reactjs “反应”;这还没有';尚未初始化-super()尚未';我不曾被称为;仅限内置,reactjs,create-react-app,Reactjs,Create React App,我正在开发一个react应用程序base con create react应用程序,该应用程序在dev服务器上运行良好,但当我运行构建时,出现了一些问题,而该应用程序不工作 我使用一个带有一些函数的HOC作为上下文,上下文中声明的函数(HOC)不起作用,因为它没有声明 如果在构建之前进行测试,那么一切都可以在dev上正常工作 this.getProducts(); 在componentDidMount上,问题将在使用此功能的下一个函数上继续 有人能帮我吗?提前谢谢 const GlobalC

我正在开发一个react应用程序base con create react应用程序,该应用程序在dev服务器上运行良好,但当我运行构建时,出现了一些问题,而该应用程序不工作

我使用一个带有一些函数的HOC作为上下文,上下文中声明的函数(HOC)不起作用,因为它没有声明

如果在构建之前进行测试,那么一切都可以在dev上正常工作

 this.getProducts();
在componentDidMount上,问题将在使用此功能的下一个函数上继续

有人能帮我吗?提前谢谢

const GlobalContext = React.createContext()

class GlobalProvider extends React.Component {

  constructor(props) {
    super(props)


    this.loadingToggle = ( status = null, where = '' ) => {
        // enable and disable loading               
    }

    this.loginFunction = (e, utente_id, password) => {
        // rest api login
    }

    this.logoutFunction = () => {
        // logout
    }


    this.getProducts = () => {

        this.forceUpdate(); 

        this.loadingToggle(true, "getProducts");

        // HERE THE PROBLEMS
        var _this = this; 

        axios.post(Config.apiBaseUrl + '/users/products', {
            token: localStorage.getItem('token')
        })
        .then( (response) => {
            if (response.data.success !== true ){
                // user not exist
            }else{
                // populate user data

                // HERE I USE _this
            }
        })
        .catch(function (error) {
            // catch error
        });

    }



    this.cartVariation = (id, qty, minQty = 0) => {
        // cart action
    }


    this.sendOrder = (addressId) => {
        // send order
    }



    this.state = { 
        isAuth: false,
        loginFunction: this.loginFunction,
        logoutFunction: this.logoutFunction,
        cartVariation: this.cartVariation,
        removeCart: this.removeCart,
        cart: null,
        forceUpdate: this.forceUpdate,
        lastUpdate: new Date().getTime(),
        cartCount: JSON.parse(localStorage.getItem("mf-cart")) !== null ? Object.keys( JSON.parse(localStorage.getItem("mf-cart"))).length : 0,
        loadingToggle: this.loadingToggle,
        loading: false,
        store : {
            mf_product_list : [],
            mf_categories : [],
            mf_users : [],
            mf_users_formatted : [],
            mf_backorders : [],
            mf_backorders_list : [],
            mf_address : []
        },
        sendOrder: this.sendOrder
    }


  }

  componentDidMount () {

    if (localStorage.getItem('token') !== null && localStorage.getItem('token-timestamp') !== null ){
        this.setState({isAuth : true});
    }
        this.getProducts();
  }


  render() {
    return (
      <GlobalContext.Provider
        value={{
          isAuth: this.state.isAuth,
          authToken: null,
          loginFunction: this.state.loginFunction,
          logoutFunction: this.state.logoutFunction,
          cartVariation: this.state.cartVariation,
          removeCart: this.state.removeCart,
          cart: null,
          forceUpdate: this.state.forceUpdate,
          lastUpdate: this.state.lastUpdate,          
          cartCount: this.state.cartCount, 
          loading: this.state.loading,
          store: this.state.store,
          sendOrder: this.sendOrder
        }}
      >
        {this.props.children}
      </GlobalContext.Provider>
    )
  }
}

const GlobalConsumer = GlobalContext.Consumer

export { GlobalProvider, GlobalConsumer }
const GlobalContext=React.createContext()
类GlobalProvider扩展了React.Component{
建造师(道具){
超级(道具)
this.loadingToggle=(状态=null,其中='')=>{
//启用和禁用加载
}
this.loginFunction=(e,utente_id,密码)=>{
//RESTAPI登录
}
this.logout函数=()=>{
//注销
}
this.getProducts=()=>{
这个.forceUpdate();
这个.loadingToggle(true,“getProducts”);
//问题就在这里
var_this=这个;
axios.post(Config.apiBaseUrl+'/users/products'{
令牌:localStorage.getItem('token')
})
。然后((响应)=>{
if(response.data.success!==true){
//用户不存在
}否则{
//填充用户数据
//我用这个
}
})
.catch(函数(错误){
//捕捉错误
});
}
this.cartVariation=(id,qty,minQty=0)=>{
//购物车行动
}
this.sendOrder=(addressId)=>{
//下单
}
this.state={
伊萨思:错,
loginFunction:this.loginFunction,
logoutFunction:this.logoutFunction,
cartVariation:this.cartVariation,
removeCart:这个,removeCart,
购物车:空,
forceUpdate:this.forceUpdate,
lastUpdate:新日期().getTime(),
cartCount:JSON.parse(localStorage.getItem(“mf cart”))!==null?Object.keys(JSON.parse(localStorage.getItem(“mf cart”)))。长度:0,
loadingToggle:this.loadingToggle,
加载:false,
商店:{
mf\U产品清单:[],
mf_类别:[],
mf_用户:[],
mf\u用户\u格式:[],
mf_延期订单:[],
mf_缺货订单清单:[],
mf_地址:[]
},
sendOrder:this.sendOrder
}
}
组件安装(){
if(localStorage.getItem('token')!==null&&localStorage.getItem('token-timestamp')!==null){
this.setState({isAuth:true});
}
这个.getProducts();
}
render(){
返回(
{this.props.children}
)
}
}
const GlobalConsumer=GlobalContext.Consumer
导出{GlobalProvider,GlobalConsumer}

从类属性更改函数绑定

this.getProducts = () => {
// ...
}
将其绑定到构造函数中(ES2015)


有关函数绑定的详细信息,请参见。

从类属性更改函数绑定

this.getProducts = () => {
// ...
}
将其绑定到构造函数中(ES2015)


有关函数绑定的详细信息如下。

您在构造函数中拥有所有内容,而super没有机会加载。您在构造函数中拥有所有内容,而super没有机会加载。