简体   繁体   English

反应js如何将输入值传递给另一个组件

[英]react js how to pass input value to another component

I have a form component named Form.js, I imported this component into Profile.js, I want to transfer the input value from the Form.js component to Profile.js (For a more illustrative example, you can see the picture https://ibb.co/tsRNCR6 ).我有一个表单组件叫Form.js,我把这个组件导入到Profile.js中,我想把Form.js组件的输入值传递给Profile.js(更直观的例子,可以看图https: //ibb.co/tsRNCR6 )。 I have deliberately removed a few lines of code from jsx to shorten the code.我特意从jsx中去掉了几行代码来缩短代码。

Profile.jsx配置文件.jsx

import React, {useState} from 'react';
import "./css/style.css"
import {Calendar} from "react-calendar";
import 'react-calendar/dist/Calendar.css';
import {DateTimeProfile} from "./Month/Clock";
import {UserCertificates} from "./ProfileContent/UserCertificate/UserCertificates";
import {AboutFormUsers} from "./ProfileContent/AboutFormUser/AboutFormUsers";

export const Profile = (props) => {

    const [value, onChange] = useState(new Date());

    if (!props.profile) {
        return <p>Loading...</p>
    }

    return (
        <div>
            <div className="contentProfile">
                <div className="userProfileInfo">

                    <div style={{margin: "20px"}}>
                        <div>
                            <AboutFormUsers {...props} />
                        </div>
                    </div>
                </div>

                <div className="achivements">
                    <div className="achivementsTitleContainer">

                        <div>
                            <div className="achivementsTitle">
                                <h3>Keep on completing achievements and become one of the best coders</h3>

                                <p>The input value should be here</p>
                            </div>
                        </div>
                    </div>

                    <div className={"centerColumnLine"}>
                        <hr/>
                    </div>
                </div>

                <div className="messages">
                    <div className="DateTimeProfile">
                        <DateTimeProfile/>
                    </div>

                    <div>
                        <Calendar onChange={onChange} value={value} className={"Calendar"}/>
                    </div>

                    <div>
                        <UserCertificates/>
                    </div>
                </div>
            </div>
        </div>
    )
}

AboutFormUsers.jsx关于FormUsers.jsx

import React from 'react';
import './css/style.css';

export class AboutFormUsers extends React.Component {

    state = {
        user: '',
    };

    handleChange = (event) => {
        const input = event.target;
        const value = input.value;

        this.setState({ [input.name]: value });
    };

    handleFormSubmit = (event) => {
        localStorage.setItem('user', this.state.user);
        event.preventDefault(); //отменяем отправку формы
    };

    componentDidMount() {
        const user = localStorage.getItem('user');
        this.setState({ user });
    }

    render() {
        return (
            <div className={"aboutFormContainer"}>
                <form onSubmit={this.handleFormSubmit}>
                    <div className={"aboutUser"}>
                        <p>Write about yourself</p>
                    </div>
                    <div>
                        <label>
                            <textarea name="user" className={"textarea"} value={this.state.user} onChange={this.handleChange}/>
                        </label>
                    </div>

                    <div>
                        <button type="submit">Sign In</button>
                    </div>

                </form>
            </div>
        );
    }
}

You can pass an event callback to AboutFormUsers component.您可以将事件回调传递给AboutFormUsers组件。 In AboutFormUsers component, call this callback when the input value is changed.AboutFormUsers组件中,当输入值更改时调用此回调。 Please check below for deail.请在下面查看详细信息。

Profile.jsx配置文件.jsx

export const Profile = props => {
  ...
  const [inputValue, setInputValue] = useState('');
  ...

  return (
    <div>
      ...
      <AboutFormUsers {...props} onChangeInput={setInputValue} />

      ...
      <div className="achivementsTitle">
        <h3>
          Keep on completing achievements and become one of the best
          coders
        </h3>

        <p>{inputValue}</p>
      </div>

      ...
    </div>
  );
};

AboutFormUsers.jsx关于FormUsers.jsx

export class AboutFormUsers extends React.Component {
  ...
  handleChange = event => {
    const input = event.target;
    const value = input.value;

    this.setState({[input.name]: value});
    this.props.onChangeInput(value);
  };
  ...
}

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

相关问题 如何在 React js 中将 onChange 输入值传递给父组件 - How to pass onChange input value to parent component in React js 如何在react js中将值从一个组件传递到另一个组件 - how to pass value from one component to another in react js 如何将值从组件内的常量变量传递到 React.js 中的另一个组件 - How to pass value from a constant variable inside a component to another component in React.js 如何将组件作为道具传递给React JS中的另一个组件? - How to pass a component as props to another component in react js? React:如何将状态值传递给另一个组件并使用它来呈现组件 - React: How to pass State value to another component and use it to render a component 如何在 React.js 中将 useState 值从一个组件传递到另一个组件 - How to pass useState Value from One Component to another one in React.js 如何将表单输入字段传递给React中的另一个组件? - How to pass form input fields to another component in React? 如何从反应组件获取输入并将输入的长度传递给另一个反应组件 - How to take the input from a react component and pass the length of the input to another react component 如何在 React.JS 中将数据从一个组件传递到另一个组件 - How to pass data from one component to another in React.JS 如何在React JS中将一个组件的值发送给另一个组件? - how to send value one component to another component in react js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM