我想在本機反應中訪問按鈕值,在純 javascript 中它看起來像:
const checkingStuff = (e) => console.log(e.target.value)
<button onclick={checkingStuff} > this is the value </button>.
如何在本機反應中實作相同的目標?
編輯:請回答本機反應,而不是反應js,因為
<Button
onPress={checkingStuff}
title='lalala123'/>
給我“未定義”。
uj5u.com熱心網友回復:
event handlersevent讓您通過該功能獲得固有的訪問權限。除非在 DOM 中專門設定狀態,否則不需要執行行內箭頭功能。您可以像這樣簡單地參考該函式:
const checkingStuff = (e) => { console.log(e.target.value) }
<button onClick={checkingStuff} > Set state and add your {value} like so </button>
uj5u.com熱心網友回復:
我主要使用以下方法:
在 TextInput 本身中。
<TextInput style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} onChangeText={text => console.log(text); value={value} />如果在 react-native 中使用 Class :
<TextInput style = {styles.input} underlineColorAndroid = "transparent" placeholder = "Password" placeholderTextColor = "#9a73ef" autoCapitalize = "none" onChangeText = {this.handlePassword}/>
而 this.handlePassword 是:
handlePassword = (text) => {
this.setState({ password: text })
}
如果您使用的是功能組件:
const [text, onChangeText] = React.useState("");
<TextInput
style={styles.input}
onChangeText={onChangeText}
value={text}
/>
快樂編碼!
uj5u.com熱心網友回復:
我不知道你試圖實作什么,但是在 ReactJs 或 React 本機按鈕中都沒有訪問 event.target.value。只有 TextInput 和類似的東西可以訪問它。你可以試試這個
const [text, onChangeText] = useState("")
const onPress = () => {
console.log(text)
}
return (
<>
<TextInput value={text} onChangeText={onChangeText}>
<Button onPress={onPress}/>
</>
)
uj5u.com熱心網友回復:
你可以得到這樣的:
const checkingStuff = (e) => console.log(e.target.value)
<button onclick={(e) => checkingStuff(e)} > this is the value </button>
uj5u.com熱心網友回復:
如果您從 react-native 匯入 Button,這可能是您的解決方案
<Button title='here is your button text' onPress={(e)=>console.log(e._dispatchInstances.memoizedProps.children[0].props.children)}/>
但是,如果您從其他來源匯入 Button,或者如果這沒有帶來確切的值,那么您可以繼續e._dispatchInstances.memoizedProps.children檢查您的按鈕文本項位置并以您自己的方式獲取它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/451784.html
標籤:javascript 反应 反应式
上一篇:如何知道react-native專案中的React和React-Native版本?
下一篇:SessionNotCreatedException:訊息:會話未創建:此版本的ChromeDriver僅支持使用Selenium和Python的Chrome版本96
