简体   繁体   中英

How to pass data from class React.Component to jsx file

I have a class with form and I want to pass entered data to next.jsx file =>

Here is class ("sender")

import React from 'react'
import Button from './Button';

class Sender extends React.Component {
    constructor() {
        super();
        this.state = {
          imie: '',
        };
        
        this.redirect = this.redirect.bind(this);
        this.handleChange = this.handleChange.bind(this);
      }
      handleChange({ target }) {
        this.setState({
          [target.name]: target.value
        });
      }
      getData(){
        const sendData = [
            {
                name: this.state.name
            }
        ]
      }
    render() {
        return (
                <div className="order-wrapper">
                    <div className="order">
                        <div className="order-box">
                            <label htmlFor="name">Imie: </label>
                            <input 
                            type="text" 
                            name="name" 
                            id="name" 
                            value={ this.state.name }
                            onChange={ this.handleChange } 
                            />
                        </div>
                        <div className="order-box">
                            <Button 
                            type="submit"
                            value="Zamów"
                            className="zamowbtn"
                            onClick={this.redirect}
                            />
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}

export default Sender

From this class I want to send name value which is located in getData() function.

I need that value in.jsx file below and show it in console.log(). How to do it? Please help me:)

import React from 'react'
import Sender from './Sender'

const Finish = () => {
  
  return (
    <div>
      { /* Here i need my 'name' value from Sender Class */ }
    </div>
  )
}

export default Finish

If there are in different parts of Application and one is not a parent of second one. You should use Context API for this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM