ReactJS: Pass Data from child to parent

ReactJS: Pass Data from child to parent

I just picked up React and can not solve this problem.

I just picked up React and can not solve this problem.

I have a search component that is embedded within the main app component. Within the search comp I make an API call with the input value from the search bar. But I do not know how I can get the Data back to the parent (where I currently use a hard coded state object similar to the data the API would return), to pass I to another component.

Basically: Get data from API within the Child, pass it to the parent and pass it down to 2 other childs


import React, {Component} from 'react';
import GroupItem from './components/GroupItem'
import SearchBar from './components/SearchBar'

class App extends Component {

state = { views: [ { name: 'Testname', description: 'testbeschreibung', status: 'ok' }, { name: 'Ein Anderer Titel', description: 'lorem ipsum', status: 'ok' } ], commands: [ { name: 'Wieder etwas', description: 'testbeschreibung', status: 'ok' } ], search : [] }

render() { return ( <div> <SearchBar/> <h2>Views </h2> {this.state.views.map((view) => (<GroupItem data={view} type={'view'} />))} <h2>Commands</h2> {this.state.commands.map((command) => (<GroupItem data={command} type={'command'} />))} </div> ); } }

export default App;

import React, {Component} from 'react';

class SearchBar extends Component {

callApi(){
    let search = this.refs.search.value;
    fetch('http://my-awesome.api/'+search)
        .then((result) =&gt; {
            return result.json();
        }).then((jsonResult) =&gt; {
        console.log(jsonResult);
    })
}

render() {
    return (
        &lt;div class="input-group mb-3" id="search"&gt;
            &lt;input type="text" class="form-control" ref="search" placeholder="URL" aria-label="URL" /&gt;
                &lt;div class="input-group-append"&gt;
                    &lt;button class="btn btn-outline-secondary" type="button" onClick={() =&gt; this.callApi()}&gt;Search&lt;/button&gt;
                &lt;/div&gt;
        &lt;/div&gt;
    );
}

}

export default SearchBar;


javascript reactjs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.

Mapbox GL Javascript wrapper for ReactJS

react-mapbox-wrapper Mapbox GL Javascript wrapper for ReactJS

JavaScript Expressions in JSX in ReactJs

Welcome, what is JSX Expressions in React Js in Hindi? JSX is an Expression Too After compilation, JSX expressions become regular JavaScript function calls a...

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.