【问题标题】:How import and export components (functions) in ReactJS?ReactJS 中如何导入导出组件(函数)?
【发布时间】:2018-04-11 00:44:16
【问题描述】:

我正在尝试在我的项目中导出和导入组件,但没有得到。

我的主应用程序代码:

import React, { Component } from 'react';
import './App.css';
import Search from './Search';

export default class App extends Component {
  constructor() {
    super();

    const peoples =[{id:0, name:"Jean"}, 
    {id:1, name:"Jaquinha"}, 
    {id:2, name:"Ricardo"}, 
    {id:3, name:"JaCA"}, 
    {id:4, name:"Letiiicia"}, 
    {id:5, name:"Dai"}, 
    {id:6, name:"Da iIIane"}, 
    {id:7, name:"Tamy"}, 
    {id:8, name:"Tamyresss"},
    {id:9, name:"Tamyres"}, 
    {id:10, name:"Abeu"}, 
    {id:11, name:"Abellll"}];
    
    this.state = {
      elementsPerPage:3,
      currentPage:0,
      peoples,
      input: "",
      filtered: peoples,
    };
    
  } 
  
  getValueInput = (evt) => {
    const inputValue = evt.target.value;
    this.setState({ input: inputValue });
    this.filterNames(inputValue);
  }
  
  filterNames = (inputValue) => {
    const { peoples } = this.state;
    this.setState({
      filtered: peoples.filter(item => 
        item.name.includes(inputValue)),
        currentPage:0
      });
}

我想为其他文件导入函数,称为Search,函数:getValueInputfilterNames,然后导入我的render()

render() {
 return (
  <div>
   <Search/>
  </div>
 )

页面,Search.js

import React, { Component } from 'react';
import {App} from './App'

export class Search extends React.Component{
    constructor(){
        super()
        
    }
    
    getValueInput = (evt) => {
        const inputValue = evt.target.value;
        this.setState({ input: inputValue });
        this.filterNames(inputValue);
    }
    
    filterNames = (inputValue) => {
        const { peoples } = this.state;
        this.setState({
            filtered: peoples.filter(item => 
                item.name.includes(inputValue)),
                currentPage:0
            });
        }
        
        render (){
            return(
                
                <div>
                <input type="text" onChange={ this.getValueInput }></input>
                <ul>Names: {this.elementsOnScreen()}</ul>
                
                </div>
            )
            
        }
    }
    
    export default Search;

但是在页面Search.js 中给出错误并且不起作用:

第 1 行:“组件”已定义但从未使用 no-unused-vars 第 2 行:“App”已定义但从未使用 no-unused-vars

我想拥有相同的功能,但文件是分开的。

有人可以帮我吗?

【问题讨论】:

  • 那些不是错误...那是你的 linter 告诉你你有 Component 和 App 变量你没有在任何地方使用。
  • 但是给出这个并且不起作用,我想导出该函数并导入其他文件以保持 App.js 更干净
  • 你测试过export default 的组件吗?您也可以使用callback function 来使用其他组件中使用的功能。
  • @salman.zare 怎么办?
  • 导出默认类搜索或从'./Search'导入{Search}

标签: reactjs


【解决方案1】:

如果您尝试在两个不同的文件中使用相同的函数,最好将该函数放入单独的文件中,然后导出/导入到App.jsSearch.js

通常,classComponent 中的函数旨在用于该类中。这是类的好处之一,因为它允许函数保持更“私有”。

这是一种处理方法:

filters.js

// Here I am creating separate functions not tied to either
// class and exporting them from the file.

const getValueInput = (evt) => {
    const inputValue = evt.target.value;
    this.setState({ input: inputValue });
    this.filterNames(inputValue);
}

const filterNames = (inputValue) => {
    const { peoples } = this.state;
    this.setState({
        filtered: peoples.filter(item => item.name.includes(inputValue)),
        currentPage: 0
    });
}

export default { getValueInput, filterNames };

App.js

import React, { Component } from 'react';
import './App.css';
import Search from './Search';

// Import the function.
import { getValueInput, filterNames } from './path/filters';

export default class App extends Component {
    constructor() {
        super();

        const peoples = [
            {id:0, name:"Jean"}, 
            {id:1, name:"Jaquinha"}, 
            {id:2, name:"Ricardo"}, 
            {id:3, name:"JaCA"}, 
            {id:4, name:"Letiiicia"}, 
            {id:5, name:"Dai"}, 
            {id:6, name:"Da iIIane"}, 
            {id:7, name:"Tamy"}, 
            {id:8, name:"Tamyresss"},
            {id:9, name:"Tamyres"}, 
            {id:10, name:"Abeu"}, 
            {id:11, name:"Abellll"}
        ];

        this.state = {
            elementsPerPage: 3,
            currentPage: 0,
            peoples,
            input: "",
            filtered: peoples,
        };

        // Bind the functions to the class.
        this.getValueInput = getValueInput.bind(this);
        this.filterNames = filterNames.bind(this);
    }

    render() {
        return (
            <div>
                <input onChange={this.getValueInput} className="someInput" />
                <Search />
            </div>
        )
    }
}

Search.js

import React, { Component } from 'react';

// import the functions.
import { getValueInput, filterNames } from './path/filters';

export class Search extends Component {
    constructor() {
        super()

        const peoples = [
            // ...
        ]

        this.state = {
            elementsPerPage: 3,
            currentPage: 0,
            peoples,
            input: "",
            filtered: peoples,
        };

        // Bind the functions to the class.
        this.getValueInput = getValueInput.bind(this);
        this.filterNames = filterNames.bind(this);
    }

    render() {
        return( 
            <div>
                <input type="text" onChange={ this.getValueInput }></input>
                <ul>Names: {this.elementsOnScreen()}</ul>
            </div>
        )       
    }
}

export default Search;

使用这种方法,您的代码将保持“DRY”,您可以在每个类中使用该函数。您确实需要确保每个类中的状态也具有适当的值。

编辑:另外,请参阅下面的评论,了解如何消除您收到的警告。

【讨论】:

  • 一切都好,但删除“应用程序”到搜索的导入。从导入中删除 Component 或扩展 Component 而不是 React.Component (当然是一样的,但它会摆脱警告)
猜你喜欢
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 2021-07-22
  • 2021-09-18
相关资源
最近更新 更多