【发布时间】: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,函数:getValueInput和filterNames,然后导入我的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