小编Oma*_*mar的帖子

如何从js文件中导入数据以在组件中使用

这个数据在自己的.js文件中我希望能够在我的应用程序中使用它我该怎么办?

   const posts = [{
          username: "lenard",
          avi: "https://scontent-sjc2-1.cdninstagram.com/t51.2885-15/e35/20905417_730036297205402_7461293070093385728_n.jpg",
        }]
Run Code Online (Sandbox Code Playgroud)

我尝试将它导入我的App.js并将其作为道具传递下去

import posts from './data/posts'; //the js file with the data
import Posts from './components/Posts/Posts'; // the component I want to use it in
class App extends Component {
  render() {
    return (
      <div className="App">
            <Navigation />
            <Posts posts={posts}/>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在Posts.js组件中使用帖子(数据)时,我在尝试映射时没有定义错误帖子

{posts.map((item) =>
Run Code Online (Sandbox Code Playgroud)

但是我不明白为什么如果我把它作为道具传下去的话它没有定义.

javascript reactjs

3
推荐指数
1
解决办法
4676
查看次数

使用void函数设置main函数中变量的值

为什么num1价值等于0?我不明白.怎么会传递num1getScore功能,并使用cin不改变价值?如何更改基于什么是它的价值cinscore.

#include <iostream>
using namespace std;

void getScore(double);

int main(int argc, const char * argv[]) {
    double num1;
    getScore(num1);
    cout << "NUM 1 got set to " << num1 << endl;
    return 0;
}

void getScore(double score) {
    cout << "whats the score";
    cin >> score;
    cout << "num is " << score << endl;
}
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
2648
查看次数

如何使用地图传递道具

我试图了解如何使用 map 函数向下传递道具。我在 renderFruits 函数中传递水果类型,在我的 Fruits 子组件中渲染水果类型。我不明白这段代码有什么问题。

import React, { Component } from 'react';
import { render } from 'react-dom';
import Fruits from'./Fruits';

class App extends Component {
  constructor(props) {
    super(props); 
    this.state = {
      fruits: [
        {
          type:'apple',
        },
        {
          type:'tomato',
        }
      ]
    };
  }
renderFruits = () => {
  const { fruits } = this.state;
    return fruits.map(item =>
      <Fruits
          type={item.type}
       />
        );
  }
  render() { 
    return (
      <div>
          {this.renderFruits}
      </div>
    );
  }
}
render(<App />, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)

水果组件,它应该渲染两个带有文本苹果和番茄的 div。

class …
Run Code Online (Sandbox Code Playgroud)

reactjs

1
推荐指数
1
解决办法
4359
查看次数

标签 统计

reactjs ×2

c++ ×1

javascript ×1