Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
React data from Children to Parent, map json

I'm learning ReactJS and I want to map a json in a father component from child search bar. So I got this:

export default class Child extends Component {
    
    constructor(props) {
        super(props)
        this.state = { data:[], value: '' };
        this.handleSubmit = this.handleSubmit.bind(this)
        this.handleChange = this.handleChange.bind(this)
    }

    guardar = (data) => {
        this.setState({ data })
        this.props.parentCallback({ data })
    }
    handleChange(e) {
        this.setState({ value: e.target.value }) 
        axios.get(`http://localhost:3001/api/search?query=${ e.target.value }`)
            .then(( { data } ) => this.guardar(data) )
    }
    handleSubmit(e) { 
        e.preventDefault()
     }

    render() {
        return(
            <form onSubmit={this.handleSubmit}>
                <input  type="text" 
                        name='searchbar'
                        onChange={this.handleChange}/>
            </form> 
        )
    }
} 


export default class Parent extends Component {
    state = {
        data: [],
    }

    handleCallback = (childData) => {
        this.setState({
            data: childData
        })
        console.log(this.state.data);
    }

    render() {
        const { data } = this.state
        return(
            <div>
                <SearchBar parentCallback = {this.handleCallback}/>
                <ProductCard />
                { [data].map( res => <li key={res.id}>{ res.title }</li>) }
            </div>
            
            
        )
    }
} 

Here is the result:

Result:

I want to map if even if the array is empty, in the console shows me the 50 elements only if I write more than twice in the input and I want them when I reload the page. Beforehand thank you very much!!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!