【问题标题】:When I click cancel button in react why the page is reloading and why its path also changing in react?当我在反应中单击取消按钮时,为什么页面正在重新加载以及为什么它的路径也在反应中改变?
【发布时间】:2020-03-24 01:56:59
【问题描述】:

我正在开发一个 React 项目,其中包含三个组件 Home、Studentlist、Card。

在学生列表组件中,我有两个按钮,一个是市场营销,另一个是计算机。

我所做的是当我点击计算机按钮时,我只会显示卡片组件。

直到这里一切都很好,在卡片组件中我有一个带有提交和取消按钮的表单

所以当我点击取消按钮时,卡片组件必须消失。

但是当我点击取消按钮时,页面正在重新加载。如何停止重新加载页面以及如何

当我点击卡片组件中的取消按钮时隐藏卡片组件而不重新加载页面

这是 Studentlist.js

import React, { useState } from 'react';
import './Studentslist.css';
import Card from '../../Components/Card/Card';

function Studentslist() {
    const [show, setShow] = useState(false);
    return (
        <div className='container'>
            <div className='row'>
                <div className='col-12'>
                    <div className='Departments'>
                        <button className='btn btn-primary'>Marketing</button>
                        <button onClick={() => setShow(true)} className='btn btn-primary ml-2'>Computers</button>
                    </div>
                    {show && <Card></Card>}
                </div>
            </div>
        </div>
    )
}

export default Studentslist

这是 Card.js

import React, { useState } from 'react';
import './Card.css';

function Card() {


    // const [show, hide] = useState(true)

    return (
        <div className='container'>
            <div className='row justify-content-center'>
                <div className='col-6'>
                    <div className='Registration'>
                        <form>
                            <div className="form-group">
                                <label htmlFor="firstname">Firstname</label>
                                <input type="text" className="form-control" id="firstname"></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="lastname">Lastname</label>
                                <input type="text" className="form-control" id="lastname"></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="email">Email</label>
                                <input type="email" className="form-control" id="email"></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="password">Password</label>
                                <input type="password" className="form-control" id="password"></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="qualification">Qualification</label>
                                <input type="text" className="form-control" id="qualification"></input>
                            </div>
                            <div className="form-group">
                                <label htmlFor="branch">Branch</label>
                                <input type="text" className="form-control" id="branch"></input>
                            </div>
                            <button type="submit" className="btn btn-primary">Submit</button>
                            <button  className='cancel btn btn-danger ml-2'>Cancel</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default Card

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    HTML 元素代表一个可点击的按钮,用于提交表单或文档中的任何位置以实现可访问的标准按钮功能。

    提交按钮是提交按钮(这是所有浏览器的默认设置,Internet Explorer 除外)

    试试

    <button type="button" className='cancel btn btn-danger ml-2'>Cancel</button>
    

    【讨论】:

    • 嗨@Tan Dat,现在如果我单击取消按钮,卡组件必须隐藏我该怎么做,我正在尝试这种方式。 const [show, hide] = useState(true)
    • 是的。试试这样吧!
    【解决方案2】:

    默认按钮操作是提交表单。如果您不需要 - 您需要防止:e.preventDefault(); 或在按钮标签中添加 type="button"

    【讨论】:

      【解决方案3】:

      您可以使用 preventDefault(),因为当您使用 type="submit" 时,页面会自动重新加载。那么,如果要关闭模态框,只需将 State 设置为 false 即可。

      function Card() {
      
         const [show, setShow] = useState(true)
      
         const closeModal = (e) => {
           e.preventDefault();
           setShow(false);
      }
      
          return (
              <div className='container'>
                  <div className='row justify-content-center'>
                      <div className='col-6'>
                          <div className='Registration'>
                              <form>
                                  <div className="form-group">
                                      <label htmlFor="firstname">Firstname</label>
                                      <input type="text" className="form-control" id="firstname"></input>
                                  </div>
                                  <div className="form-group">
                                      <label htmlFor="lastname">Lastname</label>
                                      <input type="text" className="form-control" id="lastname"></input>
                                  </div>
                                  <div className="form-group">
                                      <label htmlFor="email">Email</label>
                                      <input type="email" className="form-control" id="email"></input>
                                  </div>
                                  <div className="form-group">
                                      <label htmlFor="password">Password</label>
                                      <input type="password" className="form-control" id="password"></input>
                                  </div>
                                  <div className="form-group">
                                      <label htmlFor="qualification">Qualification</label>
                                      <input type="text" className="form-control" id="qualification"></input>
                                  </div>
                                  <div className="form-group">
                                      <label htmlFor="branch">Branch</label>
                                      <input type="text" className="form-control" id="branch"></input>
                                  </div>
                                  <button type="submit" className="btn btn-primary">Submit</button>
                                  <button  className='cancel btn btn-danger ml-2' onClick={closeModal}>Cancel</button>
                              </form>
                          </div>
                      </div>
                  </div>
              </div>
          )
      }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        • 2020-07-04
        • 2019-08-09
        相关资源
        最近更新 更多