Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda