Home > Enterprise >  How to achieve "e.target.value" in react native
How to achieve "e.target.value" in react native

Time:03-28

i wanna access button value in react native, in pure javascript it look like :

const checkingStuff = (e) => console.log(e.target.value)

<button onclick={checkingStuff} > this is the value </button>.

how to achieve the same thing in react native?

edit : please answer in react native, not react js, because

<Button
onPress={checkingStuff}
title='lalala123'/>

give me 'undefined'.

CodePudding user response:

event handlers give you inherent access to the event through the function. Unless specifically setting state within the DOM, there's no need to do an inline arrow function. You can simply reference the function like so:

const checkingStuff = (e) => { console.log(e.target.value) }

<button onClick={checkingStuff} > Set state and add your {value} like so </button>

CodePudding user response:

I don't know excatly what you try to achieve, however neither in ReactJs or React native button has access to event.target.value. Only TextInput and stuff like that have access to it. You can try this

const [text, setText] = useState("")
const onTextChange = (e) => {
   setText(e.target.value)
}
const onPress = () => {
   console.log(text)
}

return (
    <>
        <TextInput value={text} onChangeText={onTextChange}>
        <Button onPress={onPress}/>
    </>
)

CodePudding user response:

You can get like this:

const checkingStuff = (e) => console.log(e.target.value)

<button onclick={(e) => checkingStuff(e)} > this is the value </button>

CodePudding user response:

If you import Button from react-native, this can be a solution for you

<Button title='here is your button text' onPress={(e)=>console.log(e._dispatchInstances.memoizedProps.children[0].props.children)}/>

But if you import Button from other source, or if this does not bring the exact value to this, then you can go upto e._dispatchInstances.memoizedProps.children then check your button text item position and get it in your own way.

  • Related