javascript - 如何在 React 中将警报切换为常规消息
问题描述
我正在尝试更改 Dan Abraham 的代码并希望将警报切换到段落。只是想对 React 有所了解。
反应:
function FancyBorder(props) {
return ( <div className={'FancyBorder FancyBorder-' + props.color}> {props.children} </div> ); }
function Dialog(props) {
return ( <FancyBorder color="blue"> <h1 className="Dialog-title"> {props.title} </h1> <p className="Dialog-message"> {props.message} </p>
{props.children} </FancyBorder> ); }
class SignUpDialog extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSignUp = this.handleSignUp.bind(this);
this.state = {login: ''}; }
render() {
return ( <Dialog title="Mars Exploration Program" message="How should we refer to you?">
<input value={this.state.login} onChange={this.handleChange} /> <button onClick={this.handleSignUp}> Sign Me Up! </button> </Dialog> );
}
handleChange(e) { this.setState({login: e.target.value}); }
handleSignUp() { alert(`Welcome aboard, ${this.state.login}!`); }
}
ReactDOM.render( <SignUpDialog />, document.getElementById('root') );
解决方案
像这样的东西:
function FancyBorder(props) {
return (
<div className={"FancyBorder FancyBorder-" + props.color}>
{" "}
{props.children}{" "}
</div>
);
}
function Dialog(props) {
return (
<FancyBorder color="blue">
{" "}
<h1 className="Dialog-title"> {props.title} </h1>{" "}
<p className="Dialog-message"> {props.message} </p>
{props.children}{" "}
</FancyBorder>
);
}
class SignUpDialog extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSignUp = this.handleSignUp.bind(this);
this.state = {
login: "",
paragraphText: ""
};
}
render() {
return (
<Dialog
title="Mars Exploration Program"
message="How should we refer to you?"
>
<input value={this.state.login} onChange={this.handleChange} />{" "}
<button onClick={this.handleSignUp}> Sign Me Up! </button>{" "}
<p>{this.state.paragraphText}</p>
</Dialog>
);
}
handleChange(e) {
this.setState({ login: e.target.value });
}
handleSignUp() {
this.setState({
paragraphText: `Welcome aboard, ${this.state.login}!`
});
}
}
推荐阅读
- python - 如何通过其他python脚本附加列表?
- authentication - RShiny、shinymanager、RSQLite、Heroku 部署
- dpdk - dpdk 如何禁用“CRC strip”、“header split”、“IP checksum offload”和“jumbo frame support”
- python - 在 matplotlib 中使用 TextArea 和 AnnotationBbox 绘制文本
- swift - SpriteKit - 来自加载的 SKScene 的 SKShapeNode 未呈现
- node.js - 使用 Socket.IO 向用户组 Node.js 发送对象
- python - 有没有办法将科学记数法转换为 Python 中的前几位小数?
- excel - 如何在时间更新时停止指针闪烁
- lua - 如何在 Lua 中向密钥发送垃圾邮件
- php - 使用 laravel 6 在 int 上调用成员函数 lastPage()