我创建了以下课程
class App extends Component{
render() {
return (
<div className="app"></div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我如何设置初始状态?
getInitialState()不工作?我究竟做错了什么?
反应文档也没有帮助.
假设我们有这段代码
function largestOfFour(arr) {
return arr.map(Function.apply.bind(Math.max, null));
}
Run Code Online (Sandbox Code Playgroud)
其中arr是一个数组数组。
我知道使用Math.max()方法对数组进行操作时,我还必须添加apply()方法。那我Math.max.apply(null, arr)为什么会有这样的东西?apply()有什么作用?
arr.map(Function.apply.bind(Math.max, null))中,bind()的真正作用是什么?请给我我能理解的解释,我真的很感激。
请问我的反应类有这个问题,我正在尝试更新我的状态,但我得到这个错误"无法读取属性'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) 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)
有人可以解释一下这个代码如何公平地从对象中选择一个随机密钥?