javascript - 使用句柄提交和传递消息反应重定向到页面
问题描述
我正在尝试构建一个简单的网站,它接收一条消息,并在提交后将 url 中的消息传递到下一页。
以下是我到目前为止所拥有的,它不会将我的信息传递到我的welcomePage
class HomePage extends React.Component{
constructor(props) {
super(props);
this.state = {message: ''};
this.handleSubmit = this.handleSubmit.bind(this);
this.updateInput = this.updateInput.bind(this);
}
updateInput(event){
this.setState({message : event.target.value})
}
handleSubmit = e => {
e.preventDefault();
let msg = this.state.message;
let path = `/welcome/?message=${msg}/`;
console.log(path)
this.props.history.push(path) //trying to redirect to this path with the message
}
render(){
return (
<div className='HomePage'>
<header>Home</header>
<p>
Hello, please enter a welcome message:
<br/>
<input type="text"
className="form-control"
name="message"
onChange={this.updateInput}/>
<button className="submit"
type="submit"
onClick={this.handleSubmit}>
Submit
</button>
</p>
</div>
);
}
}
下面是欢迎页面,它应该从主页获取消息并显示,但现在它说欢迎,未定义而不是欢迎,当它为空时,它永远不会收到消息,因为我收到错误。
我还想如果我执行 /welcome/?message=hello%20how%20are%20you 之类的操作,欢迎页面将直接显示该消息,无论我是否进入主页进行输入。
class WelcomePage extends React.Component {
render(){
return <p>{`Welcome! ${this.input}`}</p>} //trying to get the message from the url here, so it displays
}
解决方案
从哪里来this.input
?它无处不在,所以它的价值当然是不确定的。
你应该通过解析 url 来获取消息值,你可以试试这个:
class WelcomePage extends React.Component {
render() {
const msg = new URLSearchParams(window.location.search).get('message')
return <p>{`Welcome! ${msg}`}</p>
}
}
在https://codesandbox.io/s/goofy-villani-2rnc8?file=/src/App.js中使用 react-router(v5.2.0) 的完整示例代码:
import React from "react";
import { Router, Route, Switch, withRouter } from "react-router";
import "./styles.css";
import { createBrowserHistory } from "history";
const customHistory = createBrowserHistory();
export default function App() {
return (
<Router history={customHistory}>
<Switch>
<Route exact path="/">
<HomePage />
</Route>
<Route path="/welcome">
<WelcomePage />
</Route>
</Switch>
</Router>
);
}
class _HomePage extends React.Component {
constructor(props) {
super(props);
this.state = { message: "" };
this.handleSubmit = this.handleSubmit.bind(this);
this.updateInput = this.updateInput.bind(this);
}
updateInput(event) {
this.setState({ message: event.target.value });
}
handleSubmit = e => {
e.preventDefault();
let msg = this.state.message;
let path = `/welcome?message=${msg}`;
console.log(path);
this.props.history.push(path); //trying to redirect to this path with the message
};
render() {
return (
<div className="HomePage">
<header>Home</header>
<p>
Hello, please enter a welcome message:
<br />
<input
type="text"
className="form-control"
name="message"
onChange={this.updateInput}
/>
<button className="submit" type="submit" onClick={this.handleSubmit}>
Submit
</button>
</p>
</div>
);
}
}
const HomePage = withRouter(_HomePage);
class _WelcomePage extends React.Component {
render() {
const msg = new URLSearchParams(this.props.location.search).get("message");
console.log(msg);
return <p>{`Welcome! ${msg}`}</p>;
}
}
const WelcomePage = withRouter(_WelcomePage);
推荐阅读
- shell - 外壳中的进度条
- php - 如果我更改表名,php 不会将数据插入 MySQL DB
- linux - 将 cmake 构建类型 RelWithDebInfo 剥离符号表吗?
- node.js - 如何将变量传递给 ejs 布局?
- php - 来自 SQLSTATE [42S22] 的 Laravel 应用程序错误 500:找不到列:1054 未知列
- character - 为什么 rand() 函数即使在使用种子时也会通过多次调用打印出相同的值
- python - 如何将所有 csv 文件合并为一个文件并将数据堆叠在原始标题下?
- javascript - 如果您进行更多处理,为什么以下算法之一比另一种更快?
- javascript - 回调函数返回整个对象而不是值
- r - 如何在 selectInput() 中使用具有多个级别的因子变量以在 flexdashboard 中发光