Reactjs 将带有引用的本机renderitems反应到textinput
我似乎无法理解这一点。 我想使用按钮在react native中的textinput上使用.focus()。 如何将ref传递给函数并调用该方法Reactjs 将带有引用的本机renderitems反应到textinput,reactjs,react-native,react-native-scrollview,Reactjs,React Native,React Native Scrollview,我似乎无法理解这一点。 我想使用按钮在react native中的textinput上使用.focus()。 如何将ref传递给函数并调用该方法 const [{ingredients}, dispatch] = React.useContext(AddRecipeContext); const [edit, setEdit] = React.useState(''); const startEdit = (key: string, ref: any) => { ref.c
const [{ingredients}, dispatch] = React.useContext(AddRecipeContext);
const [edit, setEdit] = React.useState('');
const startEdit = (key: string, ref: any) => {
ref.current.focus();
setEdit((prev) => (prev === key? '':key))
}
const _renderItem = ({ item }: {item: IIngredient}) => {
var textInputRef: any = React.createRef();
return (
<View key={item.key}>
<TextInput
ref={textInputRef}
onChangeText={label => setIngredientLabel(item.key, label) }
value={item.label.toString()}/>
<Pressable onPress={() => startEdit(item.key, textInputRef)}/>
</View>
);
};
const RenderIngredients = ingredients.map((item: IIngredient) => _renderItem({item}));
return (
<View>
<ScrollView>
{RenderIngredients}
</ScrollView>
</View>
);
const[{components},dispatch]=React.useContext(AddRecipeContext);
const[edit,setEdit]=React.useState(“”);
const startEdit=(key:string,ref:any)=>{
ref.current.focus();
设置编辑((上一个)=>(上一个===键?'':键))
}
常量_renderItem=({item}:{item:IIngredient})=>{
var textInputRef:any=React.createRef();
返回(
setIngredientLabel(item.key,label)}
值={item.label.toString()}/>
startEdit(item.key,textInputRef)}/>
);
};
const RenderIngredients=components.map((item:IIngredient)=>\u renderItem({item}));
返回(
{渲染元素}
);
处理裁判的正确方法是什么
先谢谢你
编辑:参考焦点();引用当前焦点() 这应该就可以了
const [{ ingredients }, dispatch] = React.useContext(AddRecipeContext);
const [edit, setEdit] = React.useState("");
const _renderItem = ({ item }: { item: IIngredient }) => {
var textInputRef: any = React.createRef();
return (
<View key={item.key}>
<TextInput
ref={textInputRef}
onChangeText={(label) => setIngredientLabel(item.key, label)}
value={item.label.toString()}
/>
<Pressable
onPress={() => {
textInputRef.current.focus();
// current was missing
setEdit((prev) => (prev === item.key ? "" : item.key));
}}
/>
</View>
);
};
const RenderIngredients = ingredients.map((item: IIngredient) =>
_renderItem({ item })
);
return (
<View>
<ScrollView>{RenderIngredients}</ScrollView>
</View>
);
const[{components},dispatch]=React.useContext(AddRecipeContext);
const[edit,setEdit]=React.useState(“”);
常量_renderItem=({item}:{item:IIngredient})=>{
var textInputRef:any=React.createRef();
返回(
setIngredientLabel(item.key,label)}
值={item.label.toString()}
/>
{
textInputRef.current.focus();
//电流不见了
setEdit((prev)=>(prev==item.key?”:item.key));
}}
/>
);
};
const RenderingElements=成分图((项目:iIngElement)=>
_renderItem({item})
);
返回(
{渲染元素}
);
您忘记了带有ref:ref.focus()的当前
对象代码>应该是ref.current.focus()代码>
这对我不起作用。它说:TypeError:null不是一个对我不起作用的对象(计算'textInputRef.current.focus')。它说:TypeError:null不是一个对象(评估'textInputRef.current.focus'),不过我会更新这个问题,谢谢。是的,这是因为在初始化时ref被设置为null
。为了避免这种情况,您应该添加一些条件,如textInputRef?.current?.focus()
。这等于if(textInputRef&&textInputRef.current){textInputRef.current.focus()}