【问题标题】:ReactJS search bar child to parentReactJS 搜索栏子到父
【发布时间】:2018-02-20 13:03:34
【问题描述】:

我有一个可以在项目的所有页面上查看的搜索栏组件。有没有一种方法可以让我拥有相同的搜索栏组件,但有一个我可以传递的属性,以便它知道要搜索哪些数据。我的搜索组件是<SearchBar type="PASS THIS VALUE" />,它被渲染了:

import React, { Component } from 'react';

class SearchBar extends Component {
  render () {


function handleClick(e) {
  e.preventDefault();
  console.log("Clicked!!!");
}
return (
<div className="row main-search">
  <div className="column">
    <form action="">
        <fieldset>
          <label htmlFor="search">
            <input type="text" placeholder="Start typing..." id="search-box" />
          </label>
        </fieldset>
      </form>
  </div>
  <div className="column">
    <div className="float-right"><button className="add-new" onClick={handleClick}>Add New</button></div>
  </div>
</div>
  )
 }
}

export default SearchBar;

如果这不可能,是否有办法让 searchBar 知道用户在哪个页面上并知道要搜索哪些数据?

更新: 我正在使用路由器元素,searchBar 组件位于父 App.js 内。我正在使用这样的路由器:

<SearchBar />
        <Route
          exact path="/" children={({ match, ...rest }) => (
            <TransitionGroup component={firstChild}>
              {match && <TourList {...rest} />}
            </TransitionGroup>
        )}/>

【问题讨论】:

  • 你在用路由器吗?可以分享SearchBar的父组件吗?
  • 我使用的是 app.js 主文件中的react-router-dom
  • 您可以将路由器 props 传递给您的 SearchBar 以便能够访问当前的路由 props 或使用像 redux 或 mobx 之类的 HOC 来处理全局应用程序状态,以使您的组件之间的通信更容易.

标签: reactjs search components


【解决方案1】:

你可以通过this.props访问props并相应地设置input的值,但要注意组件会被react flow控制,你将无法更改输入,因此你需要@987654323 @ 事件处理程序并控制该道具的本地状态版本。

可能是这样的:

import React, { Component } from 'react';

class SearchBar extends Component {
  state = { // pass default value from "type" prop
    type: this.props.type
  };
  // making sure we don't call setState excessively
  // as per https://reactjs.org/docs/react-component.html#componentwillreceiveprops
  componentWillReceiveProps(nextProps) {
    
    if (nextProps.type !== this.props.type) {
      this.setState({ type: nextProps.type });
    }
  }
  
  // pulling out handleClick function from render()
  // to avoid creating it on every render call
  handleClick = (e) => {
    e.preventDefault();
    console.log("Clicked!!!");
  }
  
  // making sure we update our controlled component state
  // so we can actually type and get the change back in the value
  // more info here: https://reactjs.org/docs/forms.html#controlled-components
  handleChange = e => {
    this.setState({ type: e.target.value });
  };
  
  render () {
    return (
      <div className="row main-search">
        <div className="column">
          <form action="">
              <fieldset>
                <label htmlFor="search">
                  <input type="text" 
                    placeholder="Start typing..." 
                    id="search-box"
                    onChange={this.handleChange}
                    // listening for our local and controlled version
                    // of "type" prop that is now reacting both to
                    // parent prop change and to local input change through typing
                    value={this.state.type}/>  
                </label>
              </fieldset>
            </form>
        </div>
        <div className="column">
          <div className="float-right">
            <button className="add-new" onClick={handleClick}>Add New</button>
          </div>
        </div>
      </div>
    )
  }
}

export default SearchBar;

【讨论】:

  • 我相信这行不通,如果有 3 个集合,搜索组件如何知道要从哪些数据中搜索?
  • 我不确定我是否明白了你的问题。 "if there a 3 set?" 是什么意思?什么意思?
  • @KarenGrigoryan 我认为 OP 是在寻求一种将道具传递给他的 SearchBar 的方法,而不是处理其中的道具的方法。
  • 我有一个搜索组件,可以搜索数据集。它搜索的数据将取决于用户所在的页面。与其拥有 SearchUserComponent 和 SearchMailComponent`,我可以只拥有 SearchComponent 并将页面路由传递给它。
  • 好吧,我知道了。在这种情况下,您不需要将路由传递给组件,您可以让您的组件像普通的 并且在组件内部您可以通过道具访问路由选项,喜欢this.props.match
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 2019-05-21
  • 1970-01-01
  • 2017-03-02
相关资源
最近更新 更多