【发布时间】:2018-09-18 13:47:48
【问题描述】:
class ReturnTempPassword extends React.Component {
constructor(props) {
super(props);
console.log(JSON.stringify(this.props));
}
render() {
return (
<div>
{ /* change code below this line */ }
<p>Your temporary password is: <strong>{}</strong></p>
{ /* change code above this line */ }
</div>
);
}
};
class ResetPassword extends React.Component {
constructor(props) {
super(props);
this.pwdGen = this.pwdGen.bind(this);
}
// returns a random string as password
pwdGen(m){
var m = m || 9, str="", r = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';;
for(var i=0; i<m; i++) {
str+= r.charAt(Math.floor(Math.random()*r.length));
}
return str;
}
render() {
return (
<div>
<h2>Reset Password</h2>
<h3>We've generated a new temporary password for you.</h3>
<h3>Please reset this password from your account settings ASAP.</h3>
{ /* change code below this line */ }
<ReturnTempPassword data={"data"} pass={this.pwdGen} />
{ /* change code above this line */ }
</div>
);
}
};
我正在以prop 的形式发送一个函数,并希望在子组件ReturnTempPassword 中访问它。但是data 可以作为道具使用,而不是pass。不知道我做错了什么?
仅供参考,这是一个 freecodecamp 任务,我正在尝试以自己的方式解决。 任务链接是here:
请帮我改正错误。
【问题讨论】:
-
我可以提供解决方案,但不知道方便吗?
-
这段代码没有错。
JSON.stringify(this.props)不会在props中显示回调函数,因为函数不是 JSON 值 json.org/value.gif。只要尝试使用this.props.pass()在任何你想要的地方实现回调函数就可以了。 -
@Kumar 感谢您指点那位朋友。这对我来说是很好的信息。
标签: javascript reactjs user-interface web-applications functional-programming