I' m building a simply React webapp. In my App.js I have the main component that accepts 3 props.(SearchCountry, SearchedCountry and Datas):
import React, { Component } from 'react';
import './App.css';
import NavBar from '../Components/NavBar';
import SideBar from '../Components/SideBar';
import CountryList from '../Components/CountryList';
import Scroll from '../Components/Scroll';
import Main from '../Components/Main';
import SearchCountry from '../Components/SearchCountry';
import SearchedCountry from '../Components/SearchedCountry';
import Datas from '../Components/Datas';
class App extends Component {
constructor() {
super();
this.state = {
nations: [],
searchField: '',
button: false
}
}
onSearchChange = (event) => {
this.setState({searchField: event.target.value})
}
onClickChange = () => {
this.setState(prevsState => ({
button: true
}));
}
render() {
const {nations, searchField, button} = this.state;
const searchedNation = nations.filter(nation => {
if(button) {
return nation.name.toLowerCase().includes(searchField.toLowerCase())
}
});
return (
<div>
<div>
<NavBar/>
</div>
<Main>
<SearchCountry searchChange={this.onSearchChange} clickChange={this.onClickChange}/>
<SearchedCountry nations={searchedNation}/>
<Datas/>
</Main>
<SideBar>
<Scroll className='scroll'>
<CountryList nations={nations}/>
</Scroll>
</SideBar>
</div>
);
}
componentDidMount() {
fetch('https://restcountries.eu/rest/v2/all')
.then(response => response.json())
.then(x => this.setState({nations: x}));
}
componentDidUpdate() {
this.state.button = false;
}
}
export default App;
This is my Main.js file:
import React from 'react';
const Main = (prop1, prop2, prop3) => {
return(
<div role='main' className='dib' style={{width: 'auto'}}>
<main>
<div className='container' style={{margin: '10px', border: '2px solid black'}}>
<div>
{prop1.children}
</div>
<div>
{prop2.children}
</div>
<div>
{prop3.children}
</div>
</div>
</main>
</div>
);
}
export default Main;
And that is the Datas.js:
import React from 'react';
const Datas = () => {
return(
<div>
<h3>Hello world</h3>
</div>
);
}
export default Datas;
I don't understand why I get this error, because the Data.js file contains something inside it and everything has been declared. Thanks in advance to anyone who gives me an answer.
Mainin theAppcomponent. See here.props.childrenis an array, you don't useprop1.children,prop2.children... you can useprops.children[0]and etc.