React setState没有设置状态

时间:2017-07-15 07:08:18

标签: reactjs material-ui

我刚刚开始研究React,我很擅长构建我正在使用的应用程序$(document).ready(function() { $('#tabelservis').DataTable( { "columnDefs": [ { "type": "numeric-comma", targets: [0, 3] } ], dom: 'Bfrtip', buttons: [ { extend: 'copyHtml5', exportOptions: { columns: [ 0, ':visible' ] } }, { extend: 'excel', exportOptions: { columns: [ 0,1, 2, 3, 4 ] } }, { extend: 'print', title: 'All of Services', exportOptions: { columns: [ 0,1, 2, 3, 4 ] }, customize: function ( win ) { $(win.document.body) .css( 'font-size', '12px' ); $(win.document.body).find( 'table' ) .css( 'font-size', '12px' ); } } ] } ); } );

我在州内material-ui没有获得价值;

action_name

当我点击“登录”按钮时,它正在调用class LoginForm extends Component { render(){return( <div> Login Form </div> )} } class SignUpForm extends Component { render(){return( <div> SignUp Form </div> )} } export class Login extends Component { static muiName = 'FlatButton'; state = { open: false, action_name: null, }; handleOpen = () => { this.setState({open: true}); this.setState({action_name: this.name}); }; handleClose = () => { this.setState({open: false}); }; render() { let form = null; let form_title = null; let action_label_cancle = null; let action_label_save = null; if (this.state.action_name === 'Login') { form_title = "Login Form"; action_label_cancle = 'Cancel'; action_label_save = 'Login'; form = <LoginForm />; } else { form_title = "SignUp Form"; action_label_cancle = 'Cancel'; action_label_save = 'SignUp'; form = <SignUpForm />; } const actions = [ <FlatButton label={action_label_cancle} primary={true} onTouchTap={this.handleClose} />, <FlatButton label={action_label_save} primary={true} keyboardFocused={true} onTouchTap={this.handleClose} />, ]; return ( <div> <FlatButton {...this.props} label="SignUp" name="SignUp" onTouchTap={this.handleOpen}/> <FlatButton {...this.props} label="Login" name="Login" onTouchTap={this.handleOpen}/> <Dialog title={form_title} modal={false} actions={actions} open={this.state.open} onRequestClose={this.handleClose}> { form } </Dialog> </div> ); } } 方法,在这种方法中,我设置handleOpen并访问action_name组件,但我得Login为{ {1}}。 任何人都可以帮助我,在哪里设置状态,以便我能够访问状态。

1 个答案:

答案 0 :(得分:1)

您收到this.state.action_name undefined因为,this.nameundefined

您可以尝试从活动中访问

  handleOpen = (event) => {
    this.setState({open: true, action_name: event.target.name});
  };

您也可以将名称作为参数传递并设置

 <FlatButton {...this.props} 
     label="SignUp" 
     name="SignUp" 
     onTouchTap={() => this.handleOpen("SignUp")}
 />
 <FlatButton {...this.props} 
     label="Login" 
     name="Login" 
     onTouchTap={() => this.handleOpen("Login")}
 />

在你的处理程序中

  handleOpen = (name) => {
    this.setState({open: true, action_name: name});
  };