相关疑难解决方法(0)

.bind(this)在ajax回调结束时的目的?

在reactjs教程中,.bind(this)在ajax回调结束时有什么目的?没有它,代码是否正常工作?

        data: JSON.stringify({text: text}),
        success: function (data) {
            this.setState({data: data});
        }.bind(this),
Run Code Online (Sandbox Code Playgroud)

ajax reactjs

58
推荐指数
1
解决办法
1万
查看次数

在React组件中创建自定义函数

我有一个React组件

export default class Archive extends React.Component { 
   ...
}
Run Code Online (Sandbox Code Playgroud)

componentDidMountonClick方法部分使用相同的代码,除了参数的微小变化.

是否可以在组件类中创建一个函数,以便可以在组件的范围内重用它?

javascript reactjs

43
推荐指数
3
解决办法
8万
查看次数

有没有办法在React Component类中绑定'this'?

我正在研究一个反应应用程序,this当组件类有很多功能时,我发现必须绑定一些麻烦.

class Foo extends Component {
  constructor(props){
    super(props);
    this.function1 = this.function1.bind(this);
    this.function2 = this.function2.bind(this);
    this.function3 = this.function3.bind(this);
  }
  function1() {
    ...
  }
  function2() {
    ...
  }
  function3() {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有更有效的方法来做到这一点?

javascript reactjs es6-class

9
推荐指数
1
解决办法
902
查看次数

React:'this.state'在组件函数中未定义

我无法访问this.state组件内的函数.我已经在SO上找到了这个问题,并将建议的代码添加到我的构造函数中:

class Game extends React.Component {

  constructor(props){
    super(props);
    ...
    this.state = {uid: '', currentTable : '', currentRound : 10, deck : sortedDeck};
    this.dealNewHand = this.dealNewHand.bind(this);
    this.getCardsForRound = this.getCardsForRound.bind(this);
    this.shuffle = this.shuffle.bind(this);
  }

  // error thrown in this function
  dealNewHand(){
    var allCardsForThisRound = this.getCardsForRound(this.state.currentRound); 
  }

  getCardsForRound(cardsPerPerson){
    var shuffledDeck = this.shuffle(sortedDeck);
    var cardsForThisRound = [];
    for(var i = 0; i < cardsPerPerson * 4; i++){
      cardsForThisRound.push(shuffledDeck[i]);
    }
    return cardsForThisRound;
  }

  shuffle(array) {
    ...
  }

  ...
  ...
Run Code Online (Sandbox Code Playgroud)

它仍然无法正常工作.this.state.currentRound …

javascript reactjs

8
推荐指数
1
解决办法
1万
查看次数

javascript:在模块内部使用strict是不必要的

tsling引发错误:

第1行:模块内部不需要'use strict'(严格)

这是我的代码

"use strict";

function Foo() {}

Foo.prototype.sayHello= function () {
    console.log("hello!");
}

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
    module.exports = { 
        Foo: Foo
    };
}
Run Code Online (Sandbox Code Playgroud)

怎么修复这个错误?

边注

我的代码同时使用module和vanilla javascript.我只想"strict mode"用于香草javascript.

也许我可以使用

if (typeof module !== 'undefined') {
    "use strict";
}
Run Code Online (Sandbox Code Playgroud)

strict mode仅启用vanilla javascript?

javascript

4
推荐指数
2
解决办法
2767
查看次数

为什么即使在编写 setState 方法后我的状态也没有得到更新?

导入 React, { Component } from 'react'

类列扩展组件{构造函数(道具){超级(道具)

    this.state={
        message:'Hello'
    }
}

changeMessage(){
    
     this.setState=({
         message:'Welcome'
    })
}

render(){
    return(
        <div>
            <div>{this.state.message}</div>
            <button onClick={this.changeMessage}>Click</button>
        </div>
    )
}
Run Code Online (Sandbox Code Playgroud)

导出默认列

javascript reactjs

2
推荐指数
1
解决办法
68
查看次数

标签 统计

javascript ×5

reactjs ×5

ajax ×1

es6-class ×1