【发布时间】:2021-09-23 19:10:53
【问题描述】:
我将状态挂钩作为道具传递给我的组件,我的 HeaderButton 工作得非常好,但是当我尝试实现 onPress 时,我收到一条错误消息,提示“props.setActiveTab 不是函数”
import React from "react";
import { useState } from "react";
import { View, Text, TouchableOpacity } from "react-native";
export default function Header(props) {
const [activeTab, setActiveTab] = useState("Delivery");
return (
<View style={{ flexDirection: "row", alignSelf: "center"}}>
<HeaderButton
text="Delivery"
bgColor="black"
textColor="white"
activeTab={activeTab}
setActiveTab={activeTab}
/>
<HeaderButton
text="Pick up"
bgColor="white"
textColor="black"
activeTab={activeTab}
setActiveTab={activeTab}
/>
</View>
);
}
const HeaderButton = (props) => (
<TouchableOpacity
style={{
backgroundColor: props.activeTab === props.text ? "black" : "white",
paddingVertical: 10,
paddingHorizontal: 40,
borderRadius: 10,
}}
onPress={() => props.setActiveTab(props.text)}
>
<Text style={{ color: props.textColor }}> {props.text}</Text>
</TouchableOpacity>
);
【问题讨论】:
标签: javascript reactjs react-native react-hooks