Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
react (native) get function/ action name via props

I am using react native and redux. Now I want to create a dynamic component, for example a dropdown component. I pass a state via props which works fine but to make this component re-usable I have to call the right dispatch function for this state. I am not sure if that is the right way to do. I am only working with react for a week now ;)

For the sake of simplicity a simple text field component:

import React, {Component} from 'react';
import {Button, StyleSheet, Text, TextInput, View} from 'react-native';
import allActions from '../../actions/allActions';
import {useDispatch, useSelector} from 'react-redux';

function Name (props) {
    const dispatch = useDispatch();
    const componentState = props.state;

    /**
     * Save the new user to the state
     * @param text
     */
    const handleChange = (text) => {
        // here I want to call the action dynamically
        // e.g. allActions.userActions.MY_VARIABLE_PASSED_THROUGH_PROPS
        dispatch(allActions.userActions.setUser(text));
    }
        return (
            <View>
                <TextInput
                    style={styles.input}
                    defaultValue={componentState}
                    onChangeText={handleChange}
                />
            </View>
        );
}

export default Name;

I pass the state when I instatiate the component

<Name state={ store.getState().user.name } action={my_action_to_call}/>

EDIT: This is my userAction

import * as actions from '../actionTypes';
const setUser = (userObj) => {
return {
    type: actions.SET_USER,
    payload: userObj
}
}
const setGender = (userObj) => {
return {
    type: actions.SET_GENDER,
    payload: userObj
}
}

export default {
setUser,
setGender
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Don't worry! That is right. for making dynamic components in react you do have to pass values and fuction via props. Your actions are fuctions, so easily you can pass your function and call it in your component. If I were you, I would definitely do that. like:

const nameAction = (name) => {
        return {
              type: SET_NAME,
              payload: name
             }
           }
<Name state={ store.getState().user.name } action={nameAction}/>
about 4 years ago · Juan Pablo Isaza Relatório

0

I figured it out:

Component call

<Dropdown title={'Gender'} placeholder={'Select your gender'} action={userActions.setGender} state={ store.getState().user.gender } data={gender_data} />

and here is the handleChange function:

    const handleChange = (e) => {
    let action = _props.action(e.value);
    dispatch(action);
    }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda