Can someone help. I have this error "Functions are not valid as a React child. This may happen if you return a Component instead of from render."
In codesandbox everything is working but when i place the code in my react app the error appears.
I am new to react so please explain how i can render EmblaCarousel...
import React, { useCallback } from "react";
import ReactDOM from "react-dom";
import EmblaCarouselReact from "embla-carousel-react";
import "components/partials/Carousel/styles.css";
function App() { {
return (
<div className="embla">
<EmblaCarouselReact
htmlTagName="div"
emblaRef={c => (emblaRef = c)}
options={{ loop: false }}
className="embla__carousel"
>
<div className="embla__container">
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
</div>
</EmblaCarouselReact>
<button onClick={() => prev()}>Previous</button>
<button onClick={() => next()}>Next</button>
</div>
);
};
}
export default App;
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
You're using a two curly bracket in here (one inside one) while defining a function, which makes it a function inside another function, rather then defining a component.
The correct code is here:
import React from "react";
import ReactDOM from "react-dom";
import EmblaCarouselReact from "embla-carousel-react";
import "components/partials/Carousel/styles.css";
function App() {
return (
<div className="embla">
<EmblaCarouselReact
htmlTagName="div"
emblaRef={c => (emblaRef = c)}
options={{ loop: false }}
className="embla__carousel"
>
<div className="embla__container">
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
</div>
</EmblaCarouselReact>
<button onClick={() => prev()}>Previous</button>
<button onClick={() => next()}>Next</button>
</div>
);
}
export default App;
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
The embla-carousel-react version you're using is highly outdated. I would recommend you to update to the latest version in order to enjoy the latest features and make sure it's compatible with later React versions:
npm install embla-carousel-react#latest --save
And make use of the useEmblaCarousel hook instead:
import React, { useCallback } from "react";
import ReactDOM from "react-dom";
import useEmblaCarousel from 'embla-carousel-react';
import "components/partials/Carousel/styles.css";
function App() {
const [emblaRef, emblaApi] = useEmblaCarousel({ loop: false });
const prev = useCallback(() => {
if (emblaApi) emblaApi.scrollPrev()
}, [emblaApi])
const next = useCallback(() => {
if (emblaApi) emblaApi.scrollNext()
}, [emblaApi])
return (
<div className="embla">
<div className="embla__carousel" ref={emblaRef}>
<div className="embla__container">
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
<div className="embla__slide">
<div className="embla__inner"></div>
</div>
</div>
</div>
<button onClick={prev}>Previous</button>
<button onClick={next}>Next</button>
</div>
);
}
export default App;
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Don't forget to add the following to your CSS because this was done inside the legacy EmblaCarouselReact component before the useEmblaCarousel hook came along:
.embla__carousel {
overflow: hidden;
}
Here is the official documentation for usage with React.
Here is the official documentation guide that shows you how to setup previous and next buttons.
Related
I'm making a small imdb clone in reactJS and I just managed to add a carousel to the component that renders out movies. The problem is that the clickevent for each movie doesn't work anymore. What's the simplest way to solve this without rewriting the entire component?
I could add a onclick event to the carousel but then i have to return all the neccessary data from the movie component and later send it to moviePage which seems redundant
Home.js
import { React } from "react";
import SearchBox from "./components/SearchBox";
import Movie from "./components/Movie";
import { Carousel } from "react-responsive-carousel";
function Home(props) {
return (
<div className="App">
<div className="row d-flex align-items-center mt-4 mb-4">
<SearchBox
searchValue={props.searchValue}
setSearchValue={props.setSearchValue}
/>
</div>
<h1>Movies</h1>
<div className="block">
<Carousel onClickItem={console.log("you pressed")}>
{props.data.map((movie, index) => {
return (
<Movie
key={movie.id}
movie={movie}
placeMovie={props.placeMovie}
index={index}
/>
);
})}
</Carousel>
</div>
<h1>Recently viewed</h1>
<div className="inline-view">
{props.latestView.map((n) => {
return (
<img
key={n.id}
src={"https://image.tmdb.org/t/p/w500" + n.backdrop_path}
alt={n.poster_path}
/>
);
})}
</div>
</div>
);
}
export default Home;
Movie.js
import React from "react";
import { NavLink } from "react-router-dom";
export default function Movie(props) {
const handleClick = () => {
props.placeMovie(props.index);
};
return (
<div className="image-container d-flex justify-content-start m-3">
<div>
<NavLink onClick={handleClick} to={`/MoviePage`}>
<img
className="image-list"
src={"https://image.tmdb.org/t/p/w500" + props.movie.backdrop_path}
alt={props.movie.path}
/>
</NavLink>
<h4 class="font-weight-bold">{props.movie.title}</h4>
<h5 class="font-weight-light">Score:{props.movie.vote_average}</h5>
</div>
</div>
);
}
Heres the moviepage that displays more info when you click on a movie item
import React from "react";
function MoviePage(props) {
return (
<div>
<div className="card-single">
<div>
<img
className="image-single"
src={"https://image.tmdb.org/t/p/w500" + props.movie.backdrop_path}
alt={props.movie.path}
/>
<div class="card-title">
<h1 class="font-weight-bold">{props.movie.title}</h1>
</div>
<h3 class="card-text">{props.movie.overview}</h3>
<h5 class="font-weight-light">
Language: {props.movie.original_language}
</h5>
<h5 class="font-weight-light">
Release date: {props.movie.release_date}
</h5>
</div>
</div>
</div>
);
}
export default MoviePage;
I'm new in ReactJs and I only know a few things as of now. I'm trying to create this Google map with directions which is successful
MapRender.js
import React from 'react';
import { render } from 'react-dom';
import Map from './mapProps';
//import './style.css';
const googleMapsApiKey = "API_KEY";
export default function FinalMap() {
const FinalMap = props => {
const {places} = props;
const {
loadingElement,
containerElement,
mapElement,
defaultCenter,
defaultZoom
} = props;
return (
<Map
googleMapURL={
'https://maps.googleapis.com/maps/api/js?key=' +
googleMapsApiKey +
'&libraries=geometry,drawing,places'
}
markers={places}
loadingElement={loadingElement || <div style={{height: `100%`}}/>}
containerElement={containerElement || <div style={{height: "80vh"}}/>}
mapElement={mapElement || <div style={{height: `100%`}}/>}
defaultCenter={defaultCenter || {lat: 25.798939, lng: -80.291409}}
defaultZoom={defaultZoom || 13}
/>
);
};
const places = [
{latitude:14.7539407,longitude:121.0338431},
{latitude: 14.625981794368357,longitude: 121.06170033978614},
]
render(<FinalMap defaultZoom={12} places={places} />, document.getElementById('root'));
}
And this is my App.js
import React from 'react';
import picture1 from '../../src/picture1.jpg';
import RechartBar from '../postlist/Rechart-Bar';
import RechartLine from '../postlist/Rechart-Line';
import driverstat from '../driverprofiles/driverstat';
import AppMap from './Maps/map';
import MapRender from './Maps/MapRender';
function Home() {
return (
<div className="Bodydiv">
<div className="homediv">
<div className="userdiv">
<div className="userdiv-profile">
<div className="userdiv-profile-photo">
<img src={picture1} className="photo" alt="picture1"></img>
</div>
<div className="userdiv-profile-name">
</div>
<div className="userdiv-profile-name">
Name:
</div>
<div className="userdiv-profile-name">
Driver ID:
</div>
<div className="userdiv-profile-name">
Plate Number:
</div>
<div className="userdiv-profile-name">
Status:
</div>
</div>
<div className="userdiv-stats">
Statistics
{driverstat.map((item, index) => {
return (
<li key={index} className={item.cName}>
{item.icon}
</li>
);
})}
<h4 class="view-history">View History</h4>
<input type="button" className="backtodrivers-btn" alt="back" value="Back"/>
</div>
</div>
<div className='mapdiv'>
<MapRender/>
</div>
</div>
<RechartBar/>
<RechartLine/>
</div>
);
}
export default Home;
The problem is It does run but it made my other elements go away because the map was rendered as seen in the last part of my MapRender.js, what I wanted to do is place this in a component and then call that component in my App.js (Similar to what I have done in RechartBar and RechartLine at the end of my App.js.
Thanks
You are replacing entire app with FinalMap component using this line.
Remove this line from MapRenderer.js
render(<FinalMap defaultZoom={12} places={places} />, document.getElementById('root'));
import React from 'react';
import { render } from 'react-dom';
import Map from './mapProps';
//import './style.css';
const googleMapsApiKey = "API_KEY";
export default function FinalMap(props) {
const {
places,
loadingElement,
containerElement,
mapElement,
defaultCenter,
defaultZoom
} = props;
return (
<Map
googleMapURL={
'https://maps.googleapis.com/maps/api/js?key=' +
googleMapsApiKey +
'&libraries=geometry,drawing,places'
}
markers={places}
loadingElement={loadingElement || <div style={{height: `100%`}}/>}
containerElement={containerElement || <div style={{height: "80vh"}}/>}
mapElement={mapElement || <div style={{height: `100%`}}/>}
defaultCenter={defaultCenter || {lat: 25.798939, lng: -80.291409}}
defaultZoom={defaultZoom || 13}
/>
);
};
Then in App.js
import React from 'react';
import picture1 from '../../src/picture1.jpg';
import RechartBar from '../postlist/Rechart-Bar';
import RechartLine from '../postlist/Rechart-Line';
import driverstat from '../driverprofiles/driverstat';
import AppMap from './Maps/map';
import MapRender from './Maps/MapRender';
function Home() {
return (
<div className="Bodydiv">
<div className="homediv">
<div className="userdiv">
<div className="userdiv-profile">
<div className="userdiv-profile-photo">
<img src={picture1} className="photo" alt="picture1"></img>
</div>
<div className="userdiv-profile-name">
</div>
<div className="userdiv-profile-name">
Name:
</div>
<div className="userdiv-profile-name">
Driver ID:
</div>
<div className="userdiv-profile-name">
Plate Number:
</div>
<div className="userdiv-profile-name">
Status:
</div>
</div>
<div className="userdiv-stats">
Statistics
{driverstat.map((item, index) => {
return (
<li key={index} className={item.cName}>
{item.icon}
</li>
);
})}
<h4 class="view-history">View History</h4>
<input type="button" className="backtodrivers-btn" alt="back" value="Back"/>
</div>
</div>
<div className='mapdiv'>
<MapRender places={[
{latitude:14.7539407,longitude:121.0338431},
{latitude: 14.625981794368357,longitude: 121.06170033978614},
]}/>
</div>
</div>
<RechartBar/>
<RechartLine/>
</div>
);
}
export default Home;
How to make collapsible onclick of <div className="content-header"> in Reactjs
As I am beginner in Reactjs how can I create a function that will handle onclick div section to make that collapsible
import React from "react";
import "./styles.css";
export default function App() {
return (
<div className="main">
<div className="content">
<div className="content-header">
<p>STEM</p>
</div>
<div className="content-body">
I am a STEM
<br></br>
I am STEM
</div>
</div>
<div className="content">
<div className="content-header">
<p>STEM</p>
</div>
<div className="content-body">
I am a STEM
<br></br>
I am STEM
</div>
</div>
</div>
);
}
This is how it look like
import React from "react";
import "./styles.css";
export default function App() {
const [isShowBody, setIsSHowBody] = React.useState(false);
const onClickHandler = () => {
setIsSHowBody(isShowBody => !isShowBody);
}
return (
<div className="main">
<div className="content">
<div className="content-header" onClick={onClickHandler}>
<p>STEM</p>
</div>
{ isShowBody && <div className="content-body">
I am a STEM
<br></br>
I am STEM
</div>}
</div>
</div>
);
}
try :
import {React,useState} from "react";
import "./styles.css";
export default function App() {
const [open,setOpen] = useState(false);
const toggle = () => {setOpen(!open)}
return (
<div className="main">
<div className="content">
<div className="content-header" onClick={toggle}>
<p>STEM</p>
</div>
{open?
<div className="content-body">
I am a STEM
<br></br>
I am STEM
</div>:null
}
</div>
</div>
);
}
if you have multiple, you have to use open array :
import {React,useState} from "react";
import "./styles.css";
export default function App() {
const [open,setOpen] = useState([false,false,false]);
const toggle = (i) => {let copyOpen = open; open[i] = !open[i] ;setOpen(copyOpen)}
return (
<div className="main">
<div className="content">
<div className="content-header" onClick={()=>toggle(0)}>
<p>STEM</p>
</div>
{open[0]?
<div className="content-body">
I am a STEM
<br></br>
I am STEM
</div>:null
}
<div className="content-header" onClick={()=>toggle(1)}>
<p>STEM</p>
</div>
{open[1]?
<div className="content-body">
I am a STEM
<br></br>
I am STEM
</div>:null
}
<div className="content-header" onClick={()=>toggle(2)}>
<p>STEM</p>
</div>
{open[2]?
<div className="content-body">
I am a STEM
<br></br>
I am STEM
</div>:null
}
</div>
</div>
);
}
I am working on react project in that project Home.js is Parent component and Test.js is Child component. In Home.js component I have tag here what I am trying to do is If I Click tag I need to call Test.js component. For this In Home.js I created one function and, To that function I passed Test.js component. and I passed that function via onClick method to h1 tag but its not working. How to achieve this.
This is App.js
import React from 'react';
import './App.css';
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import Navbar from './Components/Navbar/Navbar';
import Home from "./Pages/Home/Home";
import Test from "./Pages/Test/Test";
function App() {
return (
<div className="App">
<Router>
<Navbar></Navbar>
<Switch>
<Route exact path='/'><Home></Home></Route>
<Route path='/test'><Test></Test></Route>
</Switch>
</Router>
</div>
);
}
export default App;
This is Navbar.js
import React from 'react';
import './Navbar.css';
import { Link } from 'react-router-dom';
export default function Navbar() {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="#">Navbar</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav">
<li className="nav-item active">
<Link className='nav-link' to='/'>Home</Link>
</li>
</ul>
</div>
</nav>
</div>
</div>
</div>
)
}
This is Home.js
import React from 'react';
import './Home.css';
import { Link } from 'react-router-dom';
import Test from '../Test/Test';
export default function Home() {
const Test = () => {
<Test></Test>
}
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div>
<h1 onClick={Test}>Cruse</h1>
</div>
</div>
</div>
</div>
)
}
This is Test.js
import React from 'react'
export default function Test() {
return (
<div>
<h1>Test works</h1>
</div>
)
}
I would say you are using event handlers wrong. Their purpose is to produce some kind of side effects, and they don't return a direct result. If you wish to show new element on click i recommend using react state and something like this:
export default function Home() {
const [isTestVisible, setTestVisible] = useState(false);
const showTest= () => setTestVisible(true);
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div>
<h1 onClick={showTest}>Cruse</h1>
{isTestVisible && <Test />}
</div>
</div>
</div>
</div>
)
}
In your state you could save different props for each test component so you can customize them. Hope this helps :)
Using states is the correct answer as #Kaca992 said. But for what you do, you can do something like this:
const Test = () => {
ReactDOM.render(<Test/>, document.getElementsByTagName("h1")[0]);
}
I am working on react project and I am trying to post data to database by using axios library in react. But I am getting this kind of error --> 'customerSignup' is not defined. I am trying to solve this error, but I am not, so please help to solve this error and help me to send data from front-end to backend.
This is App.js
import React from 'react';
import './App.css';
import Navbar from './Components/Navbar/Navbar';
import Home from './Pages/Home/Home';
import Signup from './Pages/Signup/Signup';
import { BrowserRouter as Router,Switch,Route,} from "react-router-dom";
function App() {
return (
<div className="App">
<Router>
<Navbar></Navbar>
<Switch>
<Route exact path='/'><Home></Home></Route>
<Route path= '/signup'><Signup></Signup></Route>
</Switch>
</Router>
</div>
);
}
export default App;
This is Navbar.js
import React from 'react';
import './Navbar.css';
import { Link } from 'react-router-dom';
export default function Navbar() {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="#">Navbar</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav">
<li className="nav-item active">
<Link className='nav-link' to='/'>Home</Link>
</li>
<li className="nav-item">
<Link className='nav-link' to='/signup'>Signup</Link>
</li>
</ul>
</div>
</nav>
</div>
</div>
</div>
)
}
This is Signup.js
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import './Signup.css';
export default function Signup() {
const Signup = () => {
const [customerSignUp, setCustomerSignUp] = useState([
{ email: '', password: '', firstName: '', lastName: ''}
]);
const handleChange = (event) => {
setCustomerSignUp({...customerSignup, [event.target.name]: event.target.value})
}
const handleSubmit = (e) => {
e.preventDefault()
axios.post('http://localhost:9000/api/signup', customerSignup)
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.log(error)
})
}
return (
<div className="container">
<form className='white' onSubmit={handleSubmit}>
<h5 className="grey-text.text-darken-3">Sign Up With Email</h5>
<div className="input-field">
<label htmlFor="lastName">Last Name</label>
<input type="text" name="lastName" value={customerSignUp.lastName} onChange={handleChange} required />
</div>
<div className="input-field">
<button className="btn blue darken-3" type="submit">Sign Up</button>
</div>
</form>
</div>
);
}
}
`````
Your capitalization is wrong.
Change this:
const [customerSignUp, setCustomerSignUp] = ...
to this:
const [customerSignup, setCustomerSignUp] = ...
Notice the spelling difference between Signup and SignUp. Also make the changes elsewhere in the code where needed.