嗨,我是编程新手。我想不为switch语句中的默认情况返回任何链接,并且还希望当在get_type1_link中找不到任何项目时仍停留在同一页面上。我该怎么办?
下面是代码,
class ParentComponent extends React.PureComponent {
get_link = () => {
const item = this.props.item;
let link;
switch (item.type) {
case 'type_1':
link = this.get_type1_link();
break;
default:
link = '/'; //here i want to return nothing how can i do
//it
}
return link;
};
get_type1_link = () => {
const item= this.props.item;
const item_data = this.props.item_data;
const {item_id} = item_data.attributes;
const filtered_item_data = item_data && item_data.find(item_data
=>
item.id === item_id);
if (filtered_item_data) {
return `/viewer/${item_data_id}`;
} else {
return window.location.href; //if no filtered_item_data
//then want
//the user to stay on same page he is in how can i do it?
}
};
get_content = () => {
const item = this.props.item;
let text;
switch (item.type) {
case 'type1':
text = (
<span>has created item</span>
);
break;
default: text = (<span>Something</span>);
}
return text;
}
render = () => {
const item = this.props.item;
return (
<ListItem
key={item.id}
link={this.get_link}
text={this.get_content}/>
);
}
}
class ListItem extends React.PureComponent {
render = () => {
return (
<Link to={props.link()}>
<li>
<div className="text">
{props.text}
</div>
</li>
</Link>
);
};
}
在ListItem组件中,它的每个li项都有一个链接,因此,如果用户单击li项,那么在默认情况下,如果项类型为type_1,则它将页面导航到从get_type1_link接收到的链接,在所有情况下都应该没有任何链接。我该怎么做?
有人可以帮我这个忙吗?谢谢。