【发布时间】:2021-03-05 03:12:41
【问题描述】:
我是新手,最近才使用 Redux。
这是我的代码:
import React from "react";
import { View, Text, TouchableOpacity } from "react-native";
import { connect } from "react-redux";
import { useNavigation } from "@react-navigation/native";
import { controlComponent1 } from "../redux/reduce";
import style from "./style";
const Component1 = (props) => {
const navigation = useNavigation();
return (
<View
style={[
props.style,
style.container,
props.show1 ? { backgroundColor: "green" } : null
]}
>
<Text>show cai nay :v ...</Text>
<TouchableOpacity style={style.button} onPress={() => props.chance1}>
<Text>color</Text>
</TouchableOpacity>
<TouchableOpacity
style={style.button}
onPress={() => navigation.navigate("component2")}
>
<Text> next</Text>
</TouchableOpacity>
</View>
);
};
function mapStateToProps(state) {
return {
show1: state.show1
};
}
function mapDispatchToProps(dispatch) {
return {
chance1: dispatch(controlComponent1)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(Component1);
我知道mapDispatchToProps() 和mapStateToProps() 两个函数将使chance1 和show1 两个值成为我组件的道具,但我不知道如何在我的组件中使用它,因为我使用的是TypeScript .
我在这些图片上收到警告:
谁能帮我解决这个问题?
【问题讨论】:
-
您必须将
props的类型声明为包含预期道具和connect添加的道具。在我看来,使用useSelector和useDispatch挂钩更容易。 -
你能举个例子吗,我以前只使用接口,像这样:** interface Props{ style: object, show1: boolean, chance1:()=>void, } ** and添加到组件中,例如:** const Component1 = (props: Props) =>{} ** 我之前从未使用过 useSelector 和 useDispatch