简体   繁体   English

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

[英]How to transfer data from one parent component to another parent component in react using router / router params

I am new to react application development i am sending listing my code over here to get the solution there are different files 我是新来响应应用程序开发的人,我要在此处列出我的代码,以获取解决方案,其中包含不同的文件

right now i have 1 index.html and one app.js and index.js and login.js and product.js 现在我有1个index.html和一个app.js和index.js以及login.js和product.js

my app.js have routing implemented into it and index.js have browser router in it and login js have axios api call and plain login application and by successful login i am moving to another page ie product.js 我的app.js中实现了路由,index.js中具有浏览器路由器,登录js中有axios api调用和普通登录应用程序,通过成功登录,我将移至另一个页面,即product.js

so listing my files 所以列出我的文件

1) App.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 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 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) product.js 4)product.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

so i want to get the username into my product .js and print that into html tag please can someone help i want to show case PoC 所以我想将用户名输入到我的产品.js中并将其打印到html标签中,请有人可以帮助我显示案例PoC

You can pass variable with history.push 您可以通过history.push传递变量

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

and access them in product component be like: 并在product组件中访问它们就像:

this.props.location.userDetails

I hope this would be much helpful. 我希望这会有所帮助。

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

相关问题 来自其他组件的React Router访问参数 - React Router access params from another component 在React.js的父组件中使用react-router时,如何使用react context API将数据从父组件传递到子组件? - How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js? vue路由器使用模式对父组件做出反应 - vue router react on parent component using a modal 如何将数据从反应子组件传输到父组件 - How to transfer data from react child component to parent component React Router 将数据从一个组件传递到另一个组件 - React Router passing data from one component to another 角路由器如何从父组件调用子组件 - angular router how to call child component from the parent component 反应:到达组件的父级或将数据从父级传输到子级 - react: reach the component's parent or transfer data from parent to child React Router - 将选择的组件传递给父组件 - React Router - Pass chosen component to parent 使用来自 Route 组件的 react Router 设置父级的状态 - Set state of parent with react Router from Route component 将数据从子组件传输到父组件 - Transfer data from child component to parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM