【问题标题】:using componentDidMount and componentWillRecieveProps methods not able to fetch the props testHighlight from parent使用 componentDidMount 和 componentWillRecieveProps 方法无法从父级获取道具 testHighlight
【发布时间】:2019-06-08 18:53:51
【问题描述】:

我是新来的反应。我正在尝试将react-highlight-words 包含在我的应用程序中以搜索文本。所以我创建了一个简单的搜索文本框,我们可以在其中搜索文本。现在我的文本框在SearchBar.js 内,而荧光笔组件代码在Highlighter.js 内,我使用componentDidMountcomponentWillRecieveProps 从父SearchBar 组件中检索testHighlight 值。但它不打印值,所以我无法在Highlighter 内部传递。

你能告诉我如何解决它,以便将来我自己解决它。

在下面提供我的沙箱和代码 sn-p。

https://codesandbox.io/s/4qp1p2zvl4

SearchBar.js

class SearchBar extends React.Component {
  constructor() {
    super();
    this.state = {
      testHighlight: {}
    };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    console.log(e.target.value); // your search bar text
    let object = document.getElementById("myDiv");
    console.log(object.textContent); // your div text

    // now that you have the two strings you can do your search in your favorite way, for example:

    let searchBarText = e.target.value;
    console.log("searchBarText --->", searchBarText);

    let divText = object.textContent;
    console.log("divText --->", divText);

    if (divText.includes(searchBarText)) {
      console.log("the div text contains your search text");
    } else {
      console.log("the div text doesn't contain search text");
    }

    // this.setState({ testHighlight: response.data });

    this.setState({ testHighlight: searchBarText });
  }

  render() {
    return (
      <div>
        <input
          type="text"
          className="input"
          onChange={this.handleChange}
          placeholder="Search..."
          // highlightText={this.handleChange}
          testHighlight={this.state.testHighlight}
        />
      </div>
    );
  }
}

Highlighter.js

class Highlighter extends React.Component {
  constructor() {
    super();
    this.handleChange = this.handleChange.bind(this);
  }

  componentDidMount() {
    console.log("componentDidMount---->", this.props.testHighlight);
  }

  componentWillRecieveProps(nextProps) {
    console.log("componentWillRecieveProps---->", this.props.testHighlight);
    if (this.props.testHighlight !== nextProps.testHighlight) {
      this.setState({ testHighlight: nextProps.testHighlight });
    }
  }



  render() {
    return (
      <div>
        <Highlighter
          highlightClassName="YourHighlightClass"
          //searchWords={["and", "or", "the"]}
          searchWords={this.props.testHighlight}
          autoEscape={true}
          textToHighlight="The dog is chasing the cat. Or perhaps they're just playing?"
        />
      </div>
    );
  }
}

【问题讨论】:

    标签: javascript html reactjs redux material-ui


    【解决方案1】:

    现在,您还没有实现 Highlighter.js。您需要将它添加到 SearchBar.js 中的 return 方法中,并将道具传递给它,如下所示:

    <Highlighter
      testHighlight={this.state.testHighlight}
    />
    

    【讨论】:

    • 嘿,谢谢您的回复...如果我更新您的代码,我会收到错误,请在此处提供我更新的代码codesandbox.io/s/4qp1p2zvl4您可以直接在我的沙箱中修复它吗
    • 所以你有 2 个问题:1:在搜索栏中,你从 react-highlighter 导入荧光笔而不是荧光笔,所以修复你的导入 2:你不能有 2 个不同的组件都命名为荧光笔。在这种情况下,您可以摆脱您的荧光笔组件并将反应荧光笔直接导入您的 SearchBar 组件。然后直接通过searchWords。
    • 嘿,我正在尝试学习父子实现,所以我想将高亮作为子组件,已经更新了我的代码...你能帮我,以便我更好地理解 HighlighterImplementation codesandbox.io/s/4qp1p2zvl4跨度>
    • SearchBar 中的 testHighlight 应该是一个数组,而不是一个对象。您正在传递一个对象,它在其上调用“过滤器”...。过滤器只能在数组上调用。
    • 嘿,它有效,但你能告诉我如何确定何时传递对象或数组,现在如果我再次在搜索文本框中输入任何内容,我将面临错误,在此处提供更新代码codesandbox.io/s/4qp1p2zvl4
    猜你喜欢
    • 2018-10-10
    • 1970-01-01
    • 2019-12-31
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2019-05-11
    相关资源
    最近更新 更多