从另一个文件中的另一个组件更改一个组件的状态

阿努·德希隆(Anuj Dhillon)

我是React和Web开发人员的新手。我创建了一个组件,其中包含计算器列表,如按钮,该列表存储在中Buttons.js

在中还有另一个称为Submit的组件Submit.js提交组件基本上是一个文本框,在其中输入一个数学表达式,稍后我将对其进行处理。

然后,在另一个组件调用中调用这两个组件Leftwindow.js

所以我的问题是,如何使Buttons组件中的单击影响组件中的文本框Submit我知道,如果按钮和输入框是单个组件的一部分,则可以轻松完成。基本上,如果我按下“ 1”按钮,我希望将其添加到输入框中。

外观快照-

概述

代码Buttons.js-

class Buttons extends Component {
    constructor(props){
        super(props);
        this.state = {
            //buttonrows//
        };
    }

    render(){
        const row1elems = this.state.row1.map((button) => {
            return (
                <Button color={colours[button.type]} className="buttonsize">{button.label}</Button>
            );
    
        });
        const row2elems = this.state.row2.map((button) => {
            return (
                <Button color={colours[button.type]} className="buttonsize">{button.label}</Button>
            );
    
        });
        const row3elems = this.state.row3.map((button) => {
            return (
                <Button color={colours[button.type]} className="buttonsize">{button.label}</Button>
            );
    
        });
        const row4elems = this.state.row4.map((button) => {
            return (
                <Button color={colours[button.type]} className="buttonsize">{button.label}</Button>
            );
    
        });
        return (
            <div className="center">
                <ButtonGroup>
                    {row1elems}
                </ButtonGroup>
                <ButtonGroup>
                    {row2elems}
                </ButtonGroup>
                <ButtonGroup>
                    {row3elems}
                </ButtonGroup>
                <ButtonGroup>
                    {row4elems}
                </ButtonGroup>
            </div>
        );  
    }
}
export default Buttons;

代码Submit.js-

class Submit extends Component{
    constructor(props){
        super(props);
        this.state =  {
            fx: ''
        }
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleInputChange = this.handleInputChange.bind(this);
        
    }
    handleInputChange(event){
        const target = event.target;
        const val = target.val;
        const name = target.name;
        this.setState({
            [name]: val
        })
    }
    handleSubmit(event){

    }

    render(){
        return(
            <div>
            <Form onSubmit={this.handleSubmit}>
            <FormGroup row>
                <Col md={12}>
                <Input type="text" id="fx" name="fx" placeholder="Type Here" value = {this.state.fx} onChange={this.handleInputChange} />
                </Col>
            </FormGroup>
            </Form>
            <Button type="submit" color="primary">Differentiate</Button>
            </div>    
        )
    }
}
export default Submit;

的代码LeftWindow.js-

import React, { Component } from 'react';
import Buttons from './Buttons';
import './Custom.css';
import Submit from './Submit';
class LeftWindow extends Component{
    constructor(props){
        super(props);
    }
    render(){
        return(
            <div className="col-3 bg-dark fullheight">
                <Buttons/>
                <h3 className="center">Enter the function to be differentiated</h3>
                <Submit/>
            </div>
        );
    }
}
export default LeftWindow;
Shubham Prajapat

这就是您的共同祖先文件的样子-

  1. 添加了具有输入的状态。
  2. 添加了一个回调“ handleInputChange”,用于更新此输入状态。
  3. 将此回调传递给两个组件。

在此处输入图片说明

在您的Submit.js文件中,您需要使用以下命令更改输入标签

<Input 
    type="text"
    value={this.props.input}
    onChange={e => {
        this.props.handleInputChange(e.target.value);
    }}
 />

同样在Buttons.js文件中this.props.handleInputChange,单击Button单击。

<Button
    onClick={() => {
        this.props.handleInputChange(button.label)
    }} 
    color={colours[button.type]} 
    className="buttonsize"
>
    {button.label}
</Button>

而已。

希望我能帮上忙!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章