【问题标题】:Reactjs IF statementReactjs IF 语句
【发布时间】:2021-08-23 19:14:10
【问题描述】:

我有一个我调用的组件,它传递了一个记录 ID,并返回与该 ID 关联的文本。 33 应该 = 塔

将在屏幕上呈现“塔”。都很好,但是...

当我尝试在以下 IF 语句中使用该组件时,它不起作用。

...

if (<GetAssetTypeNameComponent datafromparent = {assettype_assettypeId}/> === "Tower")
{ 
    this.props.history.push(`/add-assetstower/${assetsid}/${this.props.match.params.sitemasterid}`);
}

如果我将代码更改为:使用传递的参数确实有效:

...

if (assettype_assettypeId === "33")
        { 
            this.props.history.push(`/add-assetstower/${assetsid}/${this.props.match.params.sitemasterid}`);
        }

...

我做错了什么?

罗伯

需要成为函数的组件代码.... ...

class GetAssetTypeNameComponent extends Component {
    constructor (props){
        super(props)
         this.state = {
             assettype:[]
        }
    }

    componentDidMount()
    {
        AssetTypeService.getAssetTypeById(this.props.datafromparent).then( (res) =>{
            let assettype = res.data;
            this.setState({isLoading:false});
            this.setState({
                assettypeName: assettype.assettypeName,
                assettypeType: assettype.assettypeType
            });
        });

      
    }
    render() {
        return (
            <div>
               {this.state.assettypeName}  
            </div>
        );
    }
}
export default GetAssetTypeNameComponent;

... 以下函数代码编译: ...

import React, { useState} from 'react';
import AssetTypeService from './AssetTypeService'

    const GetAssetTypeNameFunction = (props) =>{
  // destructuring 
      const { assettype_assettypeId } = props;
      const [assetType,setAssetType] = useState()
    
    AssetTypeService.getAssetTypeById(assettype_assettypeId).then( (res) =>
    setAssetType(res.data));

const arrayMap = assetType.map((post)=>{
return(
       <ul> 
           {post.assettypeName}  
        </ul>
);})

    return (
        {arrayMap}
    );
}
export default GetAssetTypeNameFunction;

... 获取执行错误: 我想是因为我从 eventHandler 中调用了该函数: ...

editAssets(assetsid,assettype_assettypeId){ if (GetAssetTypeNameFunction(assettype_assettypeId) === "Tower") { this.props.history.push(/add-assetstower/${assetsid}/${this.props.match.参数.sitemasterid}); }]

... ----- 错误:无效的挂钩调用。钩子只能在函数组件的主体内部调用。我正在响应列表中的 onClick 以根据函数 $ 路由到特定组件 我该如何解决这个问题?

【问题讨论】:

  • 嗯,=== 同时查看类型和值。 &lt;GetAssetTypeNameComponent datafromparent = {assettype_assettypeId}/&gt; 是字符串吗?在我看来,它就像一个组件。
  • @Kevin B GetAssetTypeName 是一个组件。它必须是一个返回字符串才能正常工作的函数吗?我想,因为它在渲染时显示了一个字符串,所以组件将返回一个字符串。错了吗?
  • 嗯,条件不会调用左侧的内容,它只是说,它们是否相同。在这种情况下,它们不是。

标签: reactjs string function if-statement


【解决方案1】:

组件渲染要在页面中显示的内容。渲染组件的返回值是包含您的内容的节点树。这一切意味着&lt;GetAssetTypeNameComponent&gt;可能包含文本内容Tower,但不等于字符串"Tower"。将组件渲染为此类条件的测试是没有任何意义的。

在 React 中,您想使用逻辑来告诉 React 如何渲染。您不想在您的逻辑中渲染然后使用结果。

很难给出用这么少的代码解决这个问题的最佳方法的建议,但也许你想要一个简单的函数来为你将 id 转换为一些文本。

function getAssetName(id) {
  return someLogicSomewhere(id).result.orWhatever
}

现在您可以执行以下操作:

if (getAssetName(assettype_assettypeId) === 'Tower')
{ 
    this.props.history.push(
      `/add-assetstower/${assetsid}/${this.props.match.params.sitemasterid}`
    );
}

【讨论】:

  • @ Kevin B 我将尝试将我的组件重构为一个返回字符串的函数。到目前为止,我只创建了组件。这是一个尝试的好时机。对你来说很容易,第一次需要 4 个小时.... 组件代码现在在原始帖子中列出。
  • @Kevin B 好的...在创建函数时试了一个小时(有关努力和当前错误,请参阅主要消息)。我想我很接近。在修复代码方面的任何帮助将不胜感激。一旦我掌握了函数语法的窍门,我就可以开始了!
  • @HighwayRob 您发布的代码有很多问题,很难知道从哪里开始。对于堆栈溢出帖子来说,这有点太多了。我建议学习一些基础知识。就像如何在反应应用程序中使用状态和道具一样。然后研究 async/await 编程以及如何查询外部服务。然后你可以把这两件事结合起来,在这里得到你想要的。祝你好运!
猜你喜欢
  • 2020-01-25
  • 1970-01-01
  • 2017-10-18
  • 2020-10-23
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 2022-11-13
相关资源
最近更新 更多