如何在反应功能组件中创建高阶函数
How to create higher order function in react functional component
我正在尝试使用 React 功能组件创建一个 HOC,它将采用一个组件和一些道具,但我想我遗漏了一些我没有在我传递的组件中获得道具值的东西。我也在使用打字稿
我的高阶组件:
interface EditChannelInfo {
Component: any;
setIsCollapsed: Function;
isCollapsed: boolean;
}
const EditChannelInfo = (props: EditChannelInfo): ReactElement => {
const {isCollapsed, setIsCollapsed, Component} = props;
const {data: gamesList} = useGamesList();
const games = gamesList.games.map((list: GamesList) => ({
value: list.gameId,
label: list.gameName,
}));
return <Component {...props} />;
};
export default EditChannelInfo;
从这里我将组件传递给高阶组件
import EditChannelInfoWrapper from '../EditChannelInfoWrapper';
const Dashboard: NextPage = (): ReactElement => {
const [isCollapsed, setIsCollapsed] = useState<boolean>(false);
return (
<div>
<EditChannelInfo
Component={EditChannelInfoWrapper}
setIsCollapsed={setIsCollapsed}
isCollapsed={isCollapsed}
/>
</div>
);
};
export default Dashboard;
我正在获取未定义的游戏
interface EditChannelInfoWrapper {
games: any;
}
const EditChannelInfoWrapper = (
props: EditChannelInfoWrapper,
): ReactElement => {
const {
games,
} = props;
console.log(games);
return ()
}
您似乎没有将 games
道具传递给此处的 Component
:<Component {...props} />
。
添加您的游戏道具,它应该会按预期工作<Component {...props} games={games} />
我正在尝试使用 React 功能组件创建一个 HOC,它将采用一个组件和一些道具,但我想我遗漏了一些我没有在我传递的组件中获得道具值的东西。我也在使用打字稿
我的高阶组件:
interface EditChannelInfo {
Component: any;
setIsCollapsed: Function;
isCollapsed: boolean;
}
const EditChannelInfo = (props: EditChannelInfo): ReactElement => {
const {isCollapsed, setIsCollapsed, Component} = props;
const {data: gamesList} = useGamesList();
const games = gamesList.games.map((list: GamesList) => ({
value: list.gameId,
label: list.gameName,
}));
return <Component {...props} />;
};
export default EditChannelInfo;
从这里我将组件传递给高阶组件
import EditChannelInfoWrapper from '../EditChannelInfoWrapper';
const Dashboard: NextPage = (): ReactElement => {
const [isCollapsed, setIsCollapsed] = useState<boolean>(false);
return (
<div>
<EditChannelInfo
Component={EditChannelInfoWrapper}
setIsCollapsed={setIsCollapsed}
isCollapsed={isCollapsed}
/>
</div>
);
};
export default Dashboard;
我正在获取未定义的游戏
interface EditChannelInfoWrapper {
games: any;
}
const EditChannelInfoWrapper = (
props: EditChannelInfoWrapper,
): ReactElement => {
const {
games,
} = props;
console.log(games);
return ()
}
您似乎没有将 games
道具传递给此处的 Component
:<Component {...props} />
。
添加您的游戏道具,它应该会按预期工作<Component {...props} games={games} />