[英]How to handle states in typescript?
Hello I am new to typescript and I have job to do here.您好,我是 typescript 的新手,我有工作要做。 I need to make dynamic class, something like in this code bellow.
我需要制作动态 class,类似于下面的代码。
import React, { ReactNode, useState } from "react";
interface Props {
text: ReactNode;
}
const ListItem = ({ text }: Props) => {
let [showMore, setShowMore] = useState(false);
return (
<div className="item">
<div>
<div className={`text ${showMore ? "active" : ""}`}>{text}</div>
</div>
<button onClick={() => setShowMore((s) => !s)}>Show more</button>
</div>
);
};
But I need to implement it to this code and I have no idea how to do it, because I don't understand this syntax from someone who wrote it.但是我需要在这段代码中实现它,我不知道该怎么做,因为我不理解编写它的人的这种语法。 When I try to implement previous code, I have problem with react hooks rendering in this syntax.
当我尝试实现以前的代码时,我遇到了用这种语法渲染的反应钩子的问题。
I hope you understand my problem我希望你能理解我的问题
type DeliveryBranchHitProps = {
hit: SearchIndex.DeliveryBranch;
};
const DeliveryBranchHit = ({
hit: delivery_branch,
}: DeliveryBranchHitProps): JSX.Element => (
<>
<div className={`branch-opening-week ${showMore ? "active" : ""}`}>
<p>
<span className="branch-day">Monday:</span>
{delivery_branch.opening_monday}
</p>
<button onClick={() => setShowMore((s) => !s)}>Show more</button>
</div>
</>
);
Not quite sure I understand your question, but possibly you want to do something like this where you have a generic component that takes some content chilren
.不太确定我理解你的问题,但可能你想做这样的事情,你有一个通用组件,它需要一些内容
chilren
。
interface Props {
text: ReactNode;
children: ReactNode;
}
const ListItem = ({ text, children }: Props) => {
let [showMore, setShowMore] = useState(false);
return (
<div className="item">
<div>
<div className={`text ${showMore ? "active" : ""}`}>
{text}
{children}
</div>
<button onClick={() => setShowMore((s) => !s)}>Show more</button>
</div>
</div>
);
};
type DeliveryBranchHitProps = {
hit: SearchIndex.DeliveryBranch;
};
const DeliveryBranchHit = ({ hit: delivery_branch }: DeliveryBranchHitProps): JSX.Element => (
<>
<ListItem text={delivery_branch.name} />
<p>
<span className="branch-day">Monday:</span>
{delivery_branch.opening_monday}
</p>
</ListItem>
</>
);
Please check this post to understand const func = () => (.. )
syntax Arrow function without curly braces请查看这篇文章以了解没有大括号的
const func = () => (.. )
语法箭头 function
For now your function returns a single expression - ie some JSX.现在你的 function 返回一个表达式 - 即一些 JSX。 To add some additional logic you need to make your function multiline (pay attention to the braces):
要添加一些额外的逻辑,您需要使您的 function 多行(注意大括号):
const Func = (props) => {
// your hook goes here
let [state, useState] = useState();
// return the jsx
return ( .. )
}
UPD: so for your case it is: UPD:所以对于你的情况是:
type DeliveryBranchHitProps = {
hit: SearchIndex.DeliveryBranch;
};
const DeliveryBranchHit = ({
hit: delivery_branch,
}: DeliveryBranchHitProps): JSX.Element => {
let [showMore, setShowMore] = useState(false);
return (
<>
<div className={`branch-opening-week ${showMore ? "active" : ""}`}>
<p>
<span className="branch-day">Monday:</span>
{delivery_branch.opening_monday}
</p>
<button onClick={() => setShowMore((s) => !s)}>Show more</button>
</div>
</>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.