简体   繁体   English

如何将prop传递给ReactJS中const中描述的组件

[英]How to pass props to components described in const in ReactJS

I'm using the react-sortable-hoc library to sort my list of items. 我正在使用react-sortable-hoc库对项目列表进行排序。 More than just listing I need to run a functionality when clicking the single item. 单击列表时,我不仅需要列出功能,还需要运行功能。 Listing and sorting and everything is working fine. 列表和排序,一切工作正常。 How can I pass a props that should be called clickFunction() which consoles the name when I click the name listed through SortableItem ? 当我单击通过SortableItem列出的名称时,如何传递应该称为clickFunction()的名称的道具?

import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-hoc';

const SortableItem = SortableElement(({value}) => <li>{value.first_name}</li>);

const SortableList = SortableContainer(({items}) => {
return (
    <ul>
        {items.map((value, index) => (
            <SortableItem key={`item-${index}`} index={index} value={value} />
        ))}
    </ul>
    );
});

class Details extends React.Component {

    clickFunction(name) {
        console.log(name)
    }

    onSortEnd({oldIndex, newIndex}) {
        this.setState({
          testlist: arrayMove(this.state.testlist, oldIndex, newIndex),
        });
    };

    render() {
        return (
            <div>
                <SortableList items={this.state.testlist} onSortEnd={this.onSortEnd.bind(this)} pressDelay="200" />
            </div>
        )
    }
}

You can pass the function from Details component and receive it in props of the SortableList and SortableItem like 您可以从Details组件传递函数,并在SortableList和SortableItem的道具中接收它,例如

import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-hoc';

const SortableItem = SortableElement(({value, clickFunction}) => <li onClick={() => clickFunction(value)}>{value.first_name}</li>);

const SortableList = SortableContainer(({items, clickFunction}) => {
return (
    <ul>
        {items.map((value, index) => (
            <SortableItem key={`item-${index}`} index={index} clickFunction={clickFunction} value={value} />
        ))}
    </ul>
    );
});

class Details extends React.Component {

    clickFunction(name) {
        console.log(name)
    }

    onSortEnd({oldIndex, newIndex}) {
        this.setState({
          testlist: arrayMove(this.state.testlist, oldIndex, newIndex),
        });
    };

    render() {
        return (
            <div>
                <SortableList items={this.state.testlist} onSortEnd={this.onSortEnd.bind(this)} pressDelay="200" clickFunction={this.clickFunction} />
            </div>
        )
    }
}

This is an example of passing props to SortableContainer: 这是将道具传递给SortableContainer的示例:

const SortableList = SortableContainer(({items, props, handleClickFunction, fieldSelector}) => {
    const subComponentList = [];
    items.map((value, index) =>
        subComponentList.push(
        <SortableItem 
            key={`item-${index}`} 
            index={index}
            value={value} 
            props={props} 
            handleClickFunction={handleClickFunction}
            fieldSelector={fieldSelector} 
        />)
    );

    return (
        <ul>{subComponentList}</ul>
    );
});

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

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