简体   繁体   English

将输入数据从子级传递到父级 React.js

[英]Pass input data from child to parent React.js

This may seem kind of basic but I'm just learning how to use React.这可能看起来有点基础,但我只是在学习如何使用 React。 Currently what I have going is when I type in the input field and submit, the system console logs my 'search' input.目前我要做的是当我在输入字段中输入并提交时,系统控制台会记录我的“搜索”输入。 What I'm trying to do is pass my 'search' data from my child component to the parent.我想要做的是将我的“搜索”数据从我的子组件传递给父组件。 Looking for any tips or leads to the right direction.寻找任何提示或导致正确的方向。

This is what I have for my child component:这是我为我的子组件所拥有的:

export default class SearchBar extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        search: ''
    };
}

onChange = event => {
    this.setState({ search: event.target.value });
};

onSubmit = event => {
    const { search } = this.state;
    event.preventDefault();
    console.log(search);
};

render() {
    return (
        <div className='search-bar'>
            <form onSubmit={this.onSubmit}>
                <input
                    className='search'
                    type='text'
                    placeholder='Search'
                    onChange={this.onChange}
                    search={this.props.search}
                    value={this.state.searchinput}
                    parentCallback={this.onChange}
                ></input>
            </form>
            <FontAwesomeIcon className='search-icon' icon={faSearch} />
        </div>
    );
}

} }

And in my Parent component (nothing much at the moment)在我的父组件中(目前没什么)

export default class Parent extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        search: ''
    };
}

searchUpdate = search => {
    console.log(search);
};


render() {
    console.log(this.props.search);

    return (
        <div className='container'>
            <SearchBar/>
        </div>
    );
}

} }

The simplest way would be to pass a function from parent to child:最简单的方法是将函数从父级传递给子级:

// in parent component
const setSearchValue = (search) => {
 // setState to search
 this.setState({search});
}

render (){
 return <>
  <SearchBar onsearch={this.setSearchValue} />
 </>
}


// in child component
// change your searchUpdate
searchUpdate = () => {
 const {onsearch} = this.state;
 // function call to pass data to parent
 this.props.onsearch(onsearch)
}


Just have a function that is passed as a prop to the child component.只需有一个作为道具传递给子组件的函数。 Let child component do the handle change part and pass the value back to the parent and then do whatever you want to with the value让子组件执行句柄更改部分并将值传递回父组件,然后对该值执行任何您想做的操作

Code sandbox: https://codesandbox.io/s/react-basic-example-vj3vl代码沙箱: https : //codesandbox.io/s/react-basic-example-vj3vl

Parent家长

import React from "react";
import Search from "./Search";
export default class Parent extends React.Component {

  searchUpdate = search => {
    console.log("in parent", search);
  };

  render() {
    console.log(this.props.search);

    return (
      <div className="container">
        <Search handleSearch={this.searchUpdate} />
      </div>
    );
  }
}


Child孩子

import React from "react";

export default class Search extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      search: ""
    };
  }

  onChange = event => {
    this.setState({ search: event.target.value }, () => {
      console.log("in child", this.state.search);
      this.props.handleSearch(this.state.search);
    });
  };

  onSubmit = event => {
    const { search } = this.state;
    event.preventDefault();
    console.log(search);
  };

  render() {
    return (
      <div className="search-bar">
        <form onSubmit={this.onSubmit}>
          <input
            className="search"
            type="text"
            placeholder="Search"
            onChange={this.onChange}
            search={this.props.search}
            value={this.state.searchinput}
          />
        </form>
      </div>
    );
  }
}

Generally to pass data from child component to Parent Component, you can pass a reference of a function as props to child component from parent component and call that passed function from child component with data.通常要将数据从子组件传递到父组件,您可以将函数的引用作为道具从父组件传递给子组件,并从子组件调用传递的函数和数据。

You can do something like this:你可以这样做:

export default class SearchBar extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        search: ''
    };
}

onChange = event => {
    this.setState({ search: event.target.value });
};

onSubmit = event => {
    const { search } = this.state;
    event.preventDefault();
    console.log(search);
    this.props.passSearchData(search);
};

render() {
    return (
        <div className='search-bar'>
            <form onSubmit={this.onSubmit}>
                <input
                    className='search'
                    type='text'
                    placeholder='Search'
                    onChange={this.onChange}
                    search={this.props.search}
                    value={this.state.searchinput}
                    parentCallback={this.onChange}
                ></input>
            </form>
            <FontAwesomeIcon className='search-icon' icon={faSearch} />
        </div>
    );
}

In parent component:在父组件中:

export default class Parent extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        search: ''
    };
}

searchUpdate = search => {
    console.log(search);
    this.setState({ ...state, search: search })
};


render() {
    console.log(this.props.search);

    return (
        <div className='container'>
            <SearchBar passSearchData={this.searchUpdate} />
        </div>
    );
}

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

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