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

134
Vistas
How to pass onPress to props.children?

I'm trying to make a wrapper component in react-native that I can pass down all its props to the children it wraps around. What I really want is to pass down all function props down to all its children. It looks something like this below. I want the onPress in Wrapper to be called when the TouchableOpacity is pressed.

I tried this below but it doesn't work

const Wrapper = ({children,...props})=>{
      return <View {...props}>{children}</View>
}

const App = ()=>{
    return (
        <View style={{flex:1}}>
            <Wrapper onPress={()=>{console.log(2)}}>
                <TouchableOpacity/>
            </Wrapper>
        </View>
    )
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

 const App = () => {
    return (
        <View style={{ flex: 1 }}>
            <TouchableOpacity onPress={() => {
                // do the action need here here 
            }}>


                <Wrapper  >
            </Wrapper>
        </TouchableOpacity>
            </View>
        )
    }

I would advise you to use hooks function instead

If you try to reuse functions that are related ** useAdd.js **

    export default () => {
        const addFuction(a, b) => {
            // do preprocessing here
            return a + b
        }
    
        return [addFuction]
    }

main componet

    import useAdd from "/useAdd";
    const App = () => {
        const [addFuction] = useAdd()
        return (
            <View style={{ flex: 1 }}>
                <TouchableOpacity onPress={() => {
                   addFuction(4,5)
                }}> 
                ...action component...
                      
                </TouchableOpacity>
            </View>
        )
    }

console in useAdd hook.... to see visual changes use the react useState

about 4 years ago · Juan Pablo Isaza Denunciar

0

It looks like you're looking to map the children and apply the props to each one. That might look like this:

const Wrapper = ({children,...props})=>{
  return (<>
    {React.Children.map(children, child => (
      React.cloneElement(child, {...props})
    ))}
  </>);
}

(method of mapping the children borrowed from this answer)

about 4 years ago · Juan Pablo Isaza Denunciar
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