Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

javascript - 如何通过navigationOptions内部的文本输入来更新本机组件的状态?(How to update the state for react native component from a text input inside the navigationOptions?)

I want to set the searchValue state whenever onChangeText event fires, and I want to make the value of the text input equal to the value of the state.(每当onChangeText事件触发时,我都希望设置searchValue状态,??并且我想使文本输入的值等于状态的值。)

I a trying to do that using the navigation setParam and getParams functions.(我试图使用导航setParam和getParams函数来做到这一点。) It does work, but the problem is that the component re render two times with each keystroke.(它确实可以工作,但是问题在于该组件在每次击键时都会重新渲染两次。) Here is my code:(这是我的代码:) import React, { useState, useEffect } from "react"; import { View, StyleSheet, TextInput, FlatList } from "react-native"; import { useSelector } from "react-redux"; import { Ionicons } from "@expo/vector-icons"; import { TouchableOpacity } from "react-native-gesture-handler"; import Icon from "react-native-vector-icons/MaterialCommunityIcons"; import ProductItem from "../components/shop/ProductItem"; const SearchScreen = props => { const [searchValue, setSearchValue] = useState(""); const products = useSelector(state => state.products.products); const promotionProducts = useSelector( state => state.products.promotionProducts ); const textValue = props.navigation.getParam("textValue"); useEffect(() => { setSearchValue(textValue); }, [textValue]); useEffect(() => { props.navigation.setParams({ searchValues: searchValue }); }, [searchValue]); console.log(searchValue); return ( <View> <FlatList columnWrapperStyle={styles.row} numColumns={3} data={products} keyExtractor={item => item.id} renderItem={itemData => ( <ProductItem id={itemData.item.id} image={itemData.item.mainImage} title={itemData.item.title} price={itemData.item.price} promotion={promotionProducts} weightVolumeValue={itemData.item.weightVolumeValue} weightVolumeUnit={itemData.item.weightVolumeUnit} onSelect={() => selectItemHandler(itemData.item.id, itemData.item.title) } /> )} /> </View> ); }; SearchScreen.navigationOptions = navData => { return { headerTitle: ( <View style={styles.searchContainer}> <TextInput style={styles.searchInput} placeholder="???? ???" placeholderTextColor="white" value={navData.navigation.getParam("searchValues")} onChangeText={text => navData.navigation.setParams({ textValue: text }) } /> <Icon name="magnify" size={25} style={styles.searchIcon} /> </View> ), headerStyle: { backgroundColor: "#96B1AD" }, headerLeft: ( <TouchableOpacity onPress={() => navData.navigation.goBack()}> <Ionicons name="ios-arrow-back" size={25} color="white" style={styles.backIcon} /> </TouchableOpacity> ) }; }; const styles = StyleSheet.create({ row: { flex: 1, justifyContent: "space-between" }, searchContainer: { flexDirection: "row", backgroundColor: "#334B48", flexDirection: "row", flex: 1, alignItems: "center", marginBottom: 10 }, searchIcon: { color: "white", padding: 5 }, searchInput: { flex: 1, textAlign: "right", fontFamily: "cairo-regular", fontSize: 18, color: "white" }, backIcon: { paddingLeft: 15, marginBottom: 10 } }); export default SearchScreen;   ask by Ali Zuhair translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're getting the double render because you're rerendering on the update of the searchValue in one useEffect, and updating the navigation prop in the next.(之所以得到双重渲染,是因为您要在一个useEffect中更新searchValue的值,然后在下一个useUffect中进行更新。)

Try coupling those changes in one useEffect,(尝试将这些更改结合在一起使用,) useEffect(() => { setSearchValue(textValue); props.navigation.setParams({ searchValues: textValue }); }, [textValue]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...