gpt4 book ai didi

javascript - 如何使用路由器/路由器参数在 react 中将数据从一个父组件传输到另一个父组件

转载 作者:行者123 更新时间:2023-11-28 17:23:44 25 4
gpt4 key购买 nike

我是 react 应用程序开发的新手,我正在此处发送列出我的代码以获取解决方案,有不同的文件

现在我有1个index.html和1个app.js、index.js、login.js和product.js

我的app.js已经实现了路由,index.js里面有浏览器路由器,登录js有axios api调用和普通登录应用程序,成功登录后我将移动到另一个页面,即product.js

所以列出我的文件

1) App.js

import React from 'react';
import { Switch, Route } from 'react-router-dom'
import Product from './components/product';
import Coal from './components/coal';
import Summary from './components/summary';
import Login from './components/login';

class App extends React.Component {
render() {
return (
<Switch>
<Route exact path = '/' component = {Login} />
<Route path = "/product" component = {Product} />
<Route path = "/coal" component = {Coal} />
<Route path = "/summary" component = {Summary} />
</Switch>

);
}
}

export default App;

2) Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from 'react-router-dom';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import './index.css';
import App from './App';
import Login from './components/login';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<BrowserRouter historty = {this.historty}>
<Login/>
</BrowserRouter>, document.getElementById('root'));
registerServiceWorker();

3) Login.js

import React from 'react'
import axios from 'axios';

class Login extends React.Component {
constructor(props) {
super(props)
this.state = ({
admin_id : '',
password : '',
});

this.handlePassword = this.handlePassword.bind(this);
this.handleUserName =this.handleUserName.bind(this);
this.loginUp = this.loginUp.bind(this);
}

handleUserName(e) {
e.preventDefault();
this.setState({admin_id: e.target.value})
}

handlePassword(e) {
e.preventDefault();
this.setState({password: e.target.value})
}

componentDidMount() {
}

loginUp() {

let userDetails = {
admin_id: this.state.admin_id,
password: this.state.password
}
if(this.state.admin_id === '' && this.state.password === '') {
alert('please enter fields');
} else {
axios.post("http://103.10.191.145:1337/sapl/admin/login", userDetails , this.axiosConfig)
.then((res) => {
if(res.data === 'Admin login successfully'){
this.setState({userName : userDetails.admin_id})
this.props.history.push('/product');
} else {
alert('we are not in success');
}
})
.catch((err) => {
console.log('axios error' , err);
})
}
}


render() {
return(
<div className = "container">
<div className ="row">
<div className = "col-md-12">
<h3>Login</h3>
<input className="form-control" type="text" placeholder="EmailID" value = {this.state.admin_id} onChange = {this.handleUserName} />
<input className="form-control" type="password" placeholder="password" value = {this.state.password} onChange={this.handlePassword} />
<button className="btn btn-primary" type="button" onClick={() => this.loginUp()}> Login</button>
</div>
</div>
</div>
)
}
}

export default Login

4) 产品.js

import React from 'react';

class Product extends React.Component {
constructor(props){
super(props);

}

coalOption() {

this.props.history.push('/coal');
}

componentDidMount() {
var usname = this.state;
console.log('usen' , usname);
}

render() {
return(
<div className = 'container'>
<div className = 'row'>
<div className = 'col-md-12'>
<h3>Welcome the username</h3>
<button className="btn btn-primary" type="button" onClick={() => this.coalOption()}>coal</button>
</div>
</div>
</div>

)
}
}

export default Product

所以我想将用户名放入我的产品 .js 中并将其打印到 html 标签中,请有人帮助我想展示案例 PoC

最佳答案

您可以使用history.push传递变量

this.props.history.push({
pathname: '/product',
userDetails: userDetails,
})

并在product组件中访问它们,如下所示:

this.props.location.userDetails

我希望这会很有帮助。

关于javascript - 如何使用路由器/路由器参数在 react 中将数据从一个父组件传输到另一个父组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com