是否可以檢測到Textinput組件的高度是否發生變化然后觸發某些東西。例如:
useEffect(() => {
//if the line heigh has reached 50%
console.log('Input has reach 50%')
}, []);
<TextInput styles={{maxHeight: '50%'}} multiline={true} />
我不知道從哪里開始...請幫助
uj5u.com熱心網友回復:
您可以使用useLayout來自@react-native-community/hooks 的鉤子。這允許您動態跟蹤組件的高度、寬度等。
首先安裝它
npm install @react-native-community/hooks然后在下面做這樣的事情。
import { useLayout } from '@react-native-community/hooks'
export default function App() {
// Use the hook
const { onLayout, ...layout } = useLayout()
// Check for when the height changes
useEffect(()=>{
console.log(layout.height);
},[layout.height])
// Lastly place it in the component you wish to track
return (
<>
<TextInput
onLayout={onLayout}
styles={{maxHeight: '50%'}}
multiline={true} />
</>
)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418406.html
標籤:
