[英]React re-rending manual routing works only once refreshing
I'm trying to route a component so each time button will click, new content will show up.我正在尝试路由一个组件,以便每次单击按钮时,都会显示新内容。
I have come up with the next solution:我想出了下一个解决方案:
import React, {useState} from 'react';
import {
BrowserRouter as Router,
Switch,
Route,
Link,
useHistory
} from "react-router-dom";
import Starter from '../../images/calculator.svg';
import Container from 'react-bootstrap/container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import $ from 'jquery';
function Calculator() {
const [inputName, setInputName] = useState('');
const [inputPhone, setInputPhone] = useState('');
const [inputValid, setInputValid] = useState(false);
const history = useHistory();
//Check if filled the inputs in valid way
const handleValidation = () =>{
let errors = {};
let formIsValid = true;
//Name
if(typeof inputName !== "undefined"){
if(!inputName.match(/^[a-zA-Z]+$/)){
formIsValid = false;
errors["name"] = "רק אותיות בשם, ללא מספרים או סימנים";
}
}
if(inputName === ""){
formIsValid = false;
errors["name"] = "שם מלא חסר";
}
if(typeof errors['name'] === 'undefined'){
errors['name'] = "";
}
//Phone number
if(inputPhone.length !== 9){
formIsValid = false;
errors["phone"] = "המספר אינו תקין";
}
if(typeof inputPhone !== "undefined"){
if(!inputPhone.match(/^\d+$/)){
formIsValid = false;
errors["phone"] = "רק מספרים בטלפון, ללא אותיות";
}
}
if(inputPhone === ''){
formIsValid = false;
errors["phone"] = "מספר פלאפון חסר";
}
if(typeof errors['phone'] === 'undefined'){
errors['phone'] = "";
}
$('.errors').html(errors["name"] + " <br /> " + errors["phone"]);
setInputValid(formIsValid);
return formIsValid;
}
//Handles the click
//if valid continue
//if not alert error
const handleClick = () =>{
if(inputValid){
history.push("/start");
}
else{
alert('נא למלא את תיבות הטקסט כנדרש');
}
}
return (
<section id="calculator">
<h1>
מחשבון הצעת מחיר
</h1>
<br/>
<Container>
<Router>
<Switch>
<Route exact path="/">
<div className="center">
<img src={Starter} style={{width: '500px'}} alt="מחשבון הצעת מחיר"/>
<br/>
<input onChange={handleValidation} type="text" id="name" placeholder="שם מלא" value={inputName} onInput={e => setInputName(e.target.value)} />
<br/>
<br/>
<input onChange={handleValidation} type="text" id="phone" placeholder="מספר טלפון" value={inputPhone} onInput={e => setInputPhone(e.target.value)}/>
<br/>
<p className="errors"></p>
<br/>
<button id="btn" onClick={handleClick}>התחל</button>
<br/><br/><br/>
אפשר גם לקבל הצעה בוואצאפ!
</div>
</Route>
<Route path="/start">
test
</Route>
</Switch>
</Router>
</Container>
</section>
);
}
export default Calculator;
handleValidation - handles the valid of the form, works fine, double checked it with debugging. handleValidation - 处理表单的有效性,工作正常,通过调试对其进行双重检查。
handleClick - check if the form is valid, if true execute routing, if not alert. handleClick - 检查表单是否有效,如果为真则执行路由,如果不为警报。
Each time inputValid
returns true, my website path changes to localhost:3000/start
, but only rerenders once I refreshing it.每次
inputValid
返回 true 时,我的网站路径都会更改为localhost:3000/start
,但只有在我刷新后才会localhost:3000/start
呈现。
How can I re-render an manual routing without refreshing the page?如何在不刷新页面的情况下重新渲染手动路由?
Apparently this has been answered before but was not so easy to find.显然,这之前已经回答过,但不是那么容易找到。
useHistory must have the following trigger to re-render the component routing useHistory 必须有以下触发器才能重新渲染组件路由
history.go(0);
Once I've added this to my handleClick
function it automatically re-rendered the page without refreshing it.一旦我将它添加到我的
handleClick
函数中,它会自动重新渲染页面而不刷新它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.