Javascript 有没有办法给a<;链接>;?
该页面显示从数据库获取的元素列表;关于最后显示的元素,单击它,如果它显示“Carica refereto”,我需要链接到“/upload”,如果它显示“Leggi refereto”,我需要链接到/consensi/{this.$state.item.hash_consenso}” 我尝试放置If和Else,但它响应时出错,因为它们未在react路由器dom中定义(已定义链接)Javascript 有没有办法给a<;链接>;?,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,该页面显示从数据库获取的元素列表;关于最后显示的元素,单击它,如果它显示“Carica refereto”,我需要链接到“/upload”,如果它显示“Leggi refereto”,我需要链接到/consensi/{this.$state.item.hash_consenso}” 我尝试放置If和Else,但它响应时出错,因为它们未在react路由器dom中定义(已定义链接) import React,{Component}来自“React” 从“react router dom”导入{Lin
import React,{Component}来自“React”
从“react router dom”导入{Link}
类consensiveItem扩展组件{
建造师(道具){
超级(道具);
此.state={
物品:道具
}
}
render(){
返回(
Paziente:{this.state.item.giver}
- 数据日化consenso:{this.state.item.Data\u日化}
- 数据罚款Consenso:{this.state.item.Data\u Fine}
蒂波·康森索:
{this.state.item.diritti}
)
}
}
导出默认消费品
TL;DR:在react jsx中,如果{}
内部{},则没有使用的选项。
很可能是因为您需要{}
中的运算符来返回某些内容&&
,|
和?
操作符总是返回一些东西,您可以安全地使用它们。而if
语句不返回任何内容
请阅读有关
React为您提供了几种条件渲染方法:
功能MyComponent(道具){
const isCondition=true;
常量哈希='hash';
返回(
文本
)
}
函数MyComponent(){
const isCondition=true;
常量哈希='hash';
如果(条件){
返回;
}
返回文本;
}
功能MyComponent(道具){
const isCondition=true;
常量哈希='hash';
返回(
{isCondition&&text}
{!isCondition&&text}
)
}
您需要
在返回JSX之前,还可以在render()
中设置一个变量:var upload=this.state.item.diritti==“Carica refereto”
然后使用条件呈现:{upload&&&…}
后跟{!upload&&}
改变逻辑:“如果是x,则呈现,否则呈现”。您的UI应该只呈现状态所说的应该存在的内容-逻辑应该在UI返回之前发生。这不是UI行为的一部分。谢谢Chris G!注意,“如果条件”有两个选项。一个返回完整的链接
组件,另一个(未列出)只创建to
参数,然后返回
。当然,还有最后一个未列出的选项,这是一个映射对象:
,具有模块常量映射的全局属性。
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
class ConsensiItem extends Component {
constructor(props) {
super(props);
this.state = {
item: props.item
}
}
render() {
return (
<li className="registration-form">
<ul className="container">
<li className="registration-form">Paziente :{this.state.item.giver}</li>
</ul>
<ul className="container">
<li className="registration-form">Data inizio consenso :{this.state.item.data_inizio}</li>
</ul>
<ul className="container">
<li className="registration-form">Data Fine Consenso :{this.state.item.data_fine}</li>
</ul>
<ul>
Tipo consenso :
<Link to= {'/upload'}>{this.state.item.diritti}</Link>
</ul>
<br/>
<br/>
</li>
)
}
}
export default ConsensiItem
function MyComponent(props) {
const isCondition = true;
const hash = 'hash';
return (
<Link to={isCondition ? '/upload' : `/consensi/${hash}`}>text</Link>
)
}
function MyComponent() {
const isCondition = true;
const hash = 'hash';
if (isCondition) {
return <Link to= {'/upload'}></Link>;
}
return <Link to={`/consensi/${hash}`}>text</Link>;
}
function MyComponent(props) {
const isCondition = true;
const hash = 'hash';
return (
<div>
{isCondition && <Link to= {'/upload'}>text</Link>}
{!isCondition && <Link to={`/consensi/${hash}`}>text</Link>}
</div>
)
}