简体   繁体   English

如何在 React js 中将 props 传递给组件

[英]How to pass props to components in React js

I'm working on a project with React JS , and I want to pass a props to an array, This is the Component:我正在使用React JS开发一个项目,我想将一个道具传递给一个数组,这是组件:

import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class TaskItem extends Component {
  
  render() {
    const task = this.props.task;

    return (
        <tr>
          <Link to={`/tasks/${task.id}`}>
                {task.name}
          </Link>
        </tr>
    );
  }
}

export default TaskItem;

I want the value of {task.name} to be in the title of this array:我希望{task.name}的值位于此数组的标题中:

const todoDB = {
    todos  : [
        {
        //Proident tempor est nulla irure ad est
            'id'       : '561551bd7fe2ff461101c192',
            'title'    : ' ===> HERE <===' ,
            'notes'    : 'Id nulla nulla proident deserunt deserunt proident in quis. Cillum reprehenderit labore id anim laborum.',
            'startDate': new Date(2018, 8, 3),
            'dueDate'  : new Date(2018, 8, 5),
            'completed': false,
            'starred'  : false,
            'important': false,
            'deleted'  : false,
            'labels'   : [1]
        }
]

The array and the component are not in the same file I hope those information enough to understand my problem, THANKS数组和组件不在同一个文件中我希望这些信息足以理解我的问题,谢谢

you could convert the todoDB.todos array to a function,pass the prop to the function and call it.您可以将todoDB.todos数组转换为 function,将道具传递给 function 并调用它。 sth like below:像下面的东西:

 const todoDB = { todos: (props) => [{ 'id': '561551bd7fe2ff461101c192', 'title': props, 'notes': 'Id nulla nulla proident deserunt deserunt proident in quis. Cillum reprehenderit labore id anim laborum.', 'startDate': new Date(2018, 8, 3), 'dueDate': new Date(2018, 8, 5), 'completed': false, 'starred': false, 'important': false, 'deleted': false, 'labels': [1] }] } console.log(todoDB.todos("this is the title")[0].title)

I'm not entirely clear on your requirements but the following is a function that would generate an array of tasks in the format you provided.我对您的要求并不完全清楚,但以下是 function,它将以您提供的格式生成一系列任务。

const generateTodoDB = todos => ({
    todos: todos.map(todo => ({
        'id'       : '561551bd7fe2ff461101c192',
        'title'    : todo.title,
        'notes'    : 'Id nulla ...',
        'startDate': new Date(2018, 8, 3),
        'dueDate'  : new Date(2018, 8, 5),
        'completed': false,
        'starred'  : false,
        'important': false,
        'deleted'  : false,
        'labels'   : [1]
    })
})

Now you just have to call the generateTodoDB function passing in an array of todos.现在您只需调用 generateTodoDB function 并传入一个待办事项数组。

const myTodos = [{ title: "Get Milk" }, { title: "Get Bananas" }];
const todoDB = generateTodoDB(myTodos);

generateTodoDB is a so called arrow function. generateTodoDB 是一个所谓的箭头 function。 It automatically returns the result of its function because it's body is wrapped in parenthathese.它会自动返回其 function 的结果,因为它的主体包裹在括号中。 The same is happening here with todos.map.同样的情况也发生在 todos.map 上。

const fn1 = () => ({ thisObject: "gets returned" })
const fn2 = () => { return { thisObjectDoes: "not return automatically" } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM