How to implement a search bar button and result components in a reaction

I have already seen other answers that are similar to each other, but since I'm a beginner, I tried to implement them, and I got so confused that nothing worked for me. Here is the problem:

I have a search bar (Searchbar component) that should have a submit button. When the button is pressed, the search results should appear in the Viewer component. I do not know how to connect an event from Searchbar to Viewer. How to tell one component that something has changed in another? I want two brothers and sisters to report that

import React from 'react';

var Searchbar = React.createClass({
  getInitialState: function () {
    return {};
  },

handleSubmit: function (e) {
e.preventDefault();
// what to do here
},
render: function () {
return (
  <form onSubmit={this.handleSubmit}>
    <h3>I'm looking for:</h3>
    <input ref="srch" type="search" id="search" placeholder="Search..." />
    <button>Go</button>
    <hr />
  </
});

export default Searchbar;

now for the result component:

var Result = React.createClass({

render() {
    return (
        <div>
            <hr />
            <h2>Result here</h2>
            <h2>{this.props.result.drug_name}</h2>        
            <span>{this.props.result.description}</span>
            <img src={this.props.result.image} />
        </div>
    )
}
export default Result;

They are both contained in the src folder and appear in App.js as

    var App = React.createClass({
render: function () {
    return (
      <div>
        <Searchbar />
        <Viewer />
      </div>
    )
  } 
});
+4
1

. , . , , , . - -, .

 class App extends React.Component {
    constructor() {
        super();
        this.state = {
            searchText: '',
            searchResults: []
        }
    }

    onChange(e) {
        this.setState({searchText: e.target.value});
    }

    getResults() {
        calltodb(searchText).then(e => {
            this.setState({searchResults: e.value})
        });
    }

    render() {
        return (
            <div>
                <SearchBar />
                <SearchResults />
            </div>
        )
    }
}

, . , , .

+2

Source: https://habr.com/ru/post/1671633/


All Articles