【问题标题】:React native keep getting Text string must be rendered within <Text>反应原生不断获取文本字符串必须在 <Text> 内呈现
【发布时间】:2021-06-20 16:05:02
【问题描述】:

谁能告诉我为什么我总是收到这个错误:

文本字符串必须在组件内呈现

我已取出部分 API 密钥。

这是我的代码:-

componentDidMount() {
    fetch(
        `https://api.nomics.com/v1/currencies/ticker?key=11300ce692510973c&ids&interval=5m,30d&convert=GBP&per-page=100&page=1`
      ).then((response) => response.json())
      .then((data) => {
          const ndata = data;
          const test = data.map((element) => element);
          this.setState({ new: test }); })}
  render() {
  const test = this.state.new.map((e)=>{return(<SvgUri width="10%" height="10%" source={{uri:e.logo_url}}/>)})
    return (
<View>{test}</View>
    )
  }     
  }

【问题讨论】:

    标签: javascript reactjs react-native rxjs


    【解决方案1】:

    如果 test 最终的类型为 string,则必须将 test 包装在 Text 组件中。在 react native 中,您只能将文本放在 Text 组件中

    <View>
      <Text>{test}</Text>
    <View>
    

    【讨论】:

      【解决方案2】:

      尝试这样做。

      { `test.map(logo => logo)` }

      因为这里测试

      const test = this.state.new.map((e)=&gt;{return(&lt;SvgUri width="10%" height="10%" source={{uri:e.logo_url}}/&gt;)});

      返回一个 svg 数组

      【讨论】:

      • 抱歉有点困惑我将如何实现它?
      • componentDidMount() { fetch( https://api.nomics.com/v1/currencies/ticker?key=11300ce692510973c&amp;ids&amp;interval=5m,30d&amp;convert=GBP&amp;per-page=100&amp;page=1 ).then((response) => response.json()) .then((data) => { const ndata = data; const test = data .map((element) => element); this.setState({ new: test }); })} render() { const test = this.state.new.map((e)=>{return()}) return ({ test.map(logo =&gt; logo) }) } } 尝试使用此代码
      • 这不起作用可能是代码格式?
      猜你喜欢
      • 1970-01-01
      • 2021-07-02
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 2021-07-30
      相关资源
      最近更新 更多