小编Joh*_*ere的帖子

如何在React中使用ES6类设置初始状态?

我创建了以下课程

class App extends Component{
     render() {
         return (
             <div className="app"></div>
         );
     }
}
Run Code Online (Sandbox Code Playgroud)

我如何设置初始状态?
getInitialState()不工作?我究竟做错了什么?
反应文档也没有帮助.

state reactjs

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

有人请解释Function.apply.bind(Math.max,null)算法

假设我们有这段代码

function largestOfFour(arr) {
  return arr.map(Function.apply.bind(Math.max, null));
}
Run Code Online (Sandbox Code Playgroud)

其中arr是一个数组数组。

  1. 首先,为什么我必须使用apply()?

我知道使用Math.max()方法对数组进行操作时,我还必须添加apply()方法。那我Math.max.apply(null, arr)为什么会有这样的东西?apply()有什么作用?

  1. 在这段代码arr.map(Function.apply.bind(Math.max, null))中,bind()的真正作用是什么?

请给我我能理解的解释,我真的很感激。

javascript

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

'this.setState'有什么问题

请问我的反应类有这个问题,我正在尝试更新我的状态,但我得到这个错误"无法读取属性'setState'的未定义"..我已经尝试过在线的每个解决方案,但没有运气

这是代码

export default class PageBackground extends Component{
    constructor(props) {
            super(props);

            this.state={
                lat         :'nothing',
                longi       :''
            };
            this.getLocation=this.getLocation.bind(this);
            }


        componentWillMount() {
            this.getLocation();
        }
        getLocation () {

            fetch('http://freegeoip.net/json/')
             .then((res)=>res.json())
             .then(function (objec){this.setState({lat:objec.latitude,longi:objec.longitude});})
             .catch((err)=>console.log("didn't connect to App",err))
             console.log(this.state.lat)
        }

        render(){
            return(
                <p>{this.state.lat}</p>
                )
            }
        }
Run Code Online (Sandbox Code Playgroud)

reactjs

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

这个算法如何选择随机对象密钥?(Math.random()<1/++ c)

function randomKey(obj) {
    var ret;
    var c = 0;
    for (var key in obj)
        if (Math.random() < 1/++c)
           ret = key;
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下这个代码如何公平地从对象中选择一个随机密钥?

javascript

-3
推荐指数
1
解决办法
91
查看次数

标签 统计

javascript ×2

reactjs ×2

state ×1