简体   繁体   English

e.preventDefault(); 不适用于导入的自定义按钮组件反应 js

[英]e.preventDefault(); is not working on imported custom button component react js

I am building a form and I imported a custom Button component with an onCLick.我正在构建一个表单,并使用 onCLick 导入了一个自定义 Button 组件。 In my handleClick function I added e.preventDefault as I wanted to see if the inputs from the form worked.在我的 handleClick function 中,我添加了e.preventDefault因为我想查看表单中的输入是否有效。 It worked fine yesterday when I just used a normal button, but its not working after adding the Button component.昨天我只使用普通按钮时它工作正常,但添加 Button 组件后它不起作用。 Please what am I missing here?请问我在这里错过了什么?

 import React from 'react'; import PageContainer from '../../layout/page-container/PageContainer'; import newlogin from './newlogin.scss' import Button from '../../components/button/Button'; class NewInput extends React.Component { constructor(props) { super(props); this.state = { name: '', email: '', password: '' }; } handleName = (e) => { console.log(e.target.value) this.setState ({ name: e.target.value }); } handleEmail = (e) => { console.log(e.target.value) this.setState ({ email: e.target.value // [e.target.name]: e.target.value }); } handlePassword = (e) => { console.log(e.target.value) this.setState ({ password: e.target.value }); } handleSubmit = (e) => { console.log('Submitting'); console.log(this.state); } render(){ const { email, password } = this.state; return ( <PageContainer title="Login"> <form> <input name='name' type='text' placeholder='Enter your name' value={this.state.name} onChange={this.handleName} /> <input name='email' type='text' placeholder='Enter your email' value={this.state.email} onChange={this.handleEmail} /> <input name='password' type='password' placeholder='Enter your password' value={this.state.password} onChange={this.handlePassword}/> <Button size='small' color='warning' onClick={this.handleSubmit (e.preventDefault())}>Login</Button> </form> <p>Your email is: {this.state.email}</p> </PageContainer> <:-- begin snippet: js hide: false console: true babel: false --> type Props = { children, any? type:, string? size:, string? color:, string? outline:: string } const Button = (props.Props) => { let btnClass = '' if(props;type === 'full') btnClass = 'full '. if(props;type === 'inline') btnClass = 'inline '. if(props.type === 'block') btnClass = 'block ' if(props;size === 'small') btnClass += 'small '. if(props;size === 'large') btnClass += 'large '. if(props.color === 'primary') btnClass += 'primary ' if(props.color === 'warning') btnClass += 'warning ' if(props.color === 'info') btnClass += 'info ' if(props.outline === 'transparent') btnClass += ' transparent' return ( <button className={btnClass} > {props;children}</button> ); }; export default Button;

preventDefault in handleSubmit: handleSubmit 中的 preventDefault:

handleSubmit = e => {
    e.preventDefault() // preventDefault here
    console.log('Submitting');
    console.log(this.state);
}

Call handleSubmit in Button:在 Button 中调用 handleSubmit:

onClick={this.handleSubmit}

Edit, pass props into Button like this编辑,像这样将道具传递给 Button

type Props = {
  children: any,
  type?: string,
  size?: string,
  color?: string,
  outline?: string,
  onClick?: any // define onClick in Props Type
}
const Button = (props:Props) => {
  let btnClass = "";
  ...
  ...
  return (
    <button className={btnClass} {...props}> // here
      {props.children}
    </button>
  );
};

You are not a passing the handler to the button, you are calling it passing the return value.您不是将处理程序传递给按钮,而是调用它传递返回值。 It has to be just:它必须是:

<Button size='small'  color='warning' onClick={this.handleSubmit}>Login</Button>

Then use preventDefault() inside of the handler:然后处理程序中使用preventDefault()

handleSubmit = (e) => {
  e.preventDefault();

  console.log('Submitting');
  console.log(this.state);

  // ...
}

When you submit a form, the form submit event will be fired which will refresh the page by default, so you have to hook in to the onSubmit method of the form and call the preventDefault there, in order to fire the submit event on the button click you have to set type="submit" on the Button component.当您提交表单时,将触发form提交事件,默认情况下会刷新页面,因此您必须挂钩到formonSubmit方法并在那里调用preventDefault ,以便触发按钮上的提交事件单击您必须在Button组件上设置type="submit"

Make your handleSubmit function like:使您的句柄提交handleSubmit喜欢:

  handleSubmit = (e) => {
    e.preventDefault();

    console.log('Submitting');
    console.log(this.state);
  }

and in your form use it like:并以您的形式使用它:

    <form onSubmit={this.handleSubmit}>
        // ...
        // Input fields
        // ...


        <Button type="submit" size="small"  color="warning">Login</Button>
    </form>

Make sure that the Button component passes through the type prop.确保Button组件通过type属性。

const Button = ({ children, ...props }) => (
   <button {...props}>{children}</button>
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM