Unexpected white area below html in Reactjs - reactjs

I use Reactjs and encounter an unexpected white area below the html tag:
I've checked all my .css files and cannot find any margin, padding or position attributes that might cause that problem. When I use Google Chrome inspector, I cannot inspect that white space since it's below my html tag (image above). There's no error messages indicate the cause.
My react app structure:
Here's the code:
App.js:
import React, {Component} from 'react';
import Wrapper from './components/Wrapper';
import SongCard from './components/SongCard';
import songs from './songs.json';
class App extends Component {
state = {
songs
};
render() {
return ([
<div key={0}>
<Wrapper>
{this.state.songs.map(song => (
<SongCard
key={song.id}
id={song.id}
name={song.name}
artist={song.artist}
src={song.src}
/>
))}
</Wrapper>
</div>
]);
}
}
export default App;
Wrapper.js:
import React from 'react';
import './Wrapper.css';
const Wrapper = props => (
<div className="row justify-content-center">
<div className="col-lg-8">{props.children}</div>
</div>
);
export default Wrapper;
Wrapper.css:
.row, .col-lg-8 {
margin: 0;
padding: 0;
color: white;
font-family: 'Amaranth', sans-serif;
}
SongCard.js:
import React from 'react';
import './SongCard.css';
const SongCard = props => (
<div className="song-card">
<div className="row">
<div className="col-auto spotify-player">
<iframe title={props.id} src={props.src} frameBorder="0" allowtransparency="true"></iframe>
</div>
<div className="col text-center song-info">
<div>{props.name}</div>
<div>{props.artist}</div>
</div>
</div>
<div className="song-card-divider"></div>
</div>
);
export default SongCard;
SongCard.css:
iframe {
width: 100%;
}
.col, .col-auto, .song-card {
margin: 0;
padding: 0;
background-color: #343a40;
}
.spotify-player {
width: 80px;
height: 80px;
}
.song-card-divider {
height: 0;
border-top: 1px solid #e9ecef;
}
I've looked for answers but couldn't find any answer.

I've found the cause of the problem:
It's the iframe height that causes the extra white area on the bottom of the page. I post my answer here so this question can be marked as answered and hopefully it helps people who have the same problem.

Related

How to reduce the number of product visible on my react carousel?

I have created a product carousel with react, it has 10 products, but I only want a max of 4 products to display at any one time, I tried reducing the width but that didn't work. Also had a look at the documentation for the carousel but couldn't find a solution for this https://www.npmjs.com/package/pure-react-carousel#component-properties-props
This is the git repo for the carousel https://github.com/RMP1992/react-product-carousel
react Carousel component
import React from 'react';
import {data} from'../data/data';
import { CarouselProvider, Slider, Slide, ButtonBack, ButtonNext, Image } from 'pure-react-carousel';
import './Carousel.css';
import Card from './Card';
export default class extends React.Component {
render() {
return (
<CarouselProvider
naturalSlideWidth={100}
naturalSlideHeight={125}
currentSlide={4}
totalSlides={10}
visibleSlides={4}
>
<Slider>
{data.map(item =>(
item.carouselData.map(product => (
<Slide >
<Image><img src={product.productImageUrl}/></Image>
<p>{product.name}</p>
<p>£{product.price.formattedValue}</p>
</Slide>
))
))}
</Slider>
<ButtonBack>Back</ButtonBack>
<ButtonNext>Next</ButtonNext>
</CarouselProvider>
);
}
}
CSS
.image___xtQGH {
display: block;
}
.carousel__slide {
list-style-type: none;
padding-bottom: unset !important;
width: 306px !important;
border: black solid 1px;
margin: 0 10px !important;
}
.carousel__slider-tray {
display: flex;
flex-direction: row;
}
I see you haven't imported their css yet.
Add this line to your App component and delete all your custom css as well. I think everything will work just fine.
import "pure-react-carousel/dist/react-carousel.es.css";
You can use Array#filter method to take first n elements from array.
{data.filter((item, index) => index < 4).map(item =>(
item.carouselData.map(product => (
<Slide >
<Image><img src={product.productImageUrl}/></Image>
<p>{product.name}</p>
<p>£{product.price.formattedValue}</p>
</Slide>
))
))}
Also, you can use lodash take method
https://lodash.com/docs/4.17.15#take

React do I need a hook for each component?

Problem Description
I have faced this issue multiple times and am not sure what is the best approach. Say I have 3+ components how can I have a state tracker for each of them? Do I need n useState() hooks for n components?
In this example, I want to change the image displayed with onMouseOver(). The way I did it, the hook state is applied to all the images, which is not what I desire to do.
Restrictions
I want to have eventually a custom drop-down menu with onClick() particular to the image folder. So in that sense, I need an individual state tracker for each component?
Code and Snippet
const openFolder = "https://i.postimg.cc/V6L7kBCV/folder-Open.png";
const closedFolder = "https://i.postimg.cc/hG0yn3fm/folder-Closed.png"
function App() {
const [image, setImage] = React.useState(closedFolder);
const handleMouseOver = () => {
setImage(openFolder);
}
const handleMouseLeave = () => {
setImage(closedFolder);
}
return (
<div className="window">
<div className="slider">
<div className="container">
{buildLatexFolder(image,handleMouseOver,handleMouseLeave)}
</div>
</div>
</div>
);
}
const buildLatexFolder = (image,handleMouseOver,handleMouseLeave) => {
const courseNames = [
'A', 'B', 'C', 'D', 'E', 'F'
//and many more other
]
var folders = courseNames.map((value, index) => {
return (
<div className="folderContainer">
<span>{value}</span>
<div onMouseEnter={() => {handleMouseOver()}} onMouseLeave={() => {handleMouseLeave()}}>
<img src={image} width="130"/>
</div>
</div>
);
});
return folders;
}
ReactDOM.render(<App />, document.querySelector("#app"));
.window {
width: 500px;
height: 130px;
display: flex;
align-items: center;
margin-left: auto;
margin-right: auto;
}
.slider {
border-radius: 0px 0px 16px 16px;
background-color: #1F1F1F;
color: #ffffff;
overflow: hidden;
float: right;
}
.container {
display: flex;
width: 700px;
height: 130px;
margin-top: 10px;
align-items: center;
text-align: center;
}
.folderElement {
margin-left: 12px;
margin-right: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="app"></div>
with JSFiddle here
What I Have Tried
Previously, when faced with this problem I would just make, say, 3 hooks for my 3 components and handle the logic this way. But now I have a large number of components and don't know what is the best approach.
I have searched online 'React Should I have a hook for each component', 'How many hooks to have for components', and so on and so forth but the search results are inaccurate with what I am trying to find, or for that matter my question is inaccurate. I don't know how to proceed.
Does anyone know how I can fix this issue?
You need to have component modularization in your application. Then you can have a single CourseFolder which can be used as a child component resides in your parent component which is App. Using map, you can view multiple child components having different courseDetails inside them.
Refer the following code-snippets I created to solve your issue.
App.js
import React from "react";
import CourseFolder from "./CourseFolder";
// import "./folderStyles.css"; /* kept the styles posted in the question */
const courseNames = [
"A",
"B",
"C",
"D",
"E",
"F",
//and many more other
];
function App() {
return (
<div className="window">
<div className="slider">
<div className="container">
{courseNames.map((value, index) => (
<CourseFolder value={value} key={index} />
))}
</div>
</div>
</div>
);
}
export default App;
Then create the child component as follows.
CourseFolder.js
import React from "react";
const openFolder = "https://i.postimg.cc/V6L7kBCV/folder-Open.png";
const closedFolder = "https://i.postimg.cc/hG0yn3fm/folder-Closed.png";
function CourseFolder(props) {
const { value, key } = props;
const [image, setImage] = React.useState(closedFolder);
const handleMouseOver = () => {
setImage(openFolder);
};
const handleMouseLeave = () => {
setImage(closedFolder);
};
return (
<div className="folderContainer" key={key}>
<span>{value}</span>
<div
onMouseEnter={() => {
handleMouseOver();
}}
onMouseLeave={() => {
handleMouseLeave();
}}
>
<img src={image} width="130" alt="alternative" />
</div>
</div>
);
}
export default CourseFolder;
Hope this would be helpful to solve your issue.

ReactJs using styles css module on react-burger-menu component

I am working with react-burger-menu and I am having trouble setting the styles={styles} tag on the menu component, from a CSS module.
Given :
./App.js
import Page from './components/Page/Page';
import SideMenu from './components/sideMenu/SideMenu.js';
import styles from './components/sideMenu/SideMenu.module.css';
function App() {
return (
<div className="app" id="app">
<SideMenu styles={ styles } pageWrapId={ "page-wrap" } outerContainerId={ "app" }/>
<div id="page-wrap">
<Page />
</div>
</div>
);
}
export default App;
./sideMenu/SideMenu.js
import React from "react";
import { scaleRotate as Menu } from "react-burger-menu";
export default props => {
return (
<Menu {...props} >
<a className="menu-item" href="/">
Home
</a>
</Menu>
);
};
./sideMenu/SideMenu.module.css
.bm-burger-button {
position: fixed;
width: 36px;
height: 30px;
right: 36px;
top: 36px;
}
.bm-burger-bars {
background: #373a47;
}
I am unable to pass the prop styles to SideMenu menu bar. The stylesheet is just not read. I have also tried style={styles} instead. If I omit the styles={styles} tag and just put the CSS styling in ./index.css then the styling is rendered correctly but I want to separate the menu styling as a module.
I have also tried to write the
import styles from '../../components/sideMenu/SideMenu.module.css'
inside the sideMenu/SideMenu.js file instead, and return (<Menu {...props} styles={styles}> (...) but still with no success.
Finally, I have also tried to set
<div className="app" id="app" styles={styles}> (...) </div>
in ./App.js as it was suggested in this post but that doesn't work either.
Does anyone know what I am doing wrong?
First, it’s recommend to use camelCase for classes with more than one word. The reason for this is that you’ll access these classes later as properties of a JS object, hyphens are not syntactically allowed. So you must first of all change your SideMenu.module.css for example like this:
.bm_burger_button {
position: fixed;
width: 36px;
height: 30px;
right: 36px;
top: 36px;
}
.bm_burger_bars {
background: #373a47;
}
Import the module you’ve just created from within your component, like you did:
import styles from './components/sideMenu/SideMenu.module.css';
To use a class defined in your module, just refer to it as a normal property from the styles object, like:
<div className={styles.bm_burger_button}> (...) </div>

Custom button in toolbar draftjs editor

How can I add a button with a custom function behind it to the draftjs editor? I have the feeling this should be pretty straightforward but I can't find examples or documentation.
I have an Editor with only bold italic and list buttons and I want to add a button that shows a popup where I can select 1 of 3 options and then "inject" this in the editor.
Can someone point me in the right direction (or documentation)?
here how i tried this, purely react hooks way to achieving things,
App.js file
import MyEditor from './Component/MyEditor';
function App() {
<MyEditor />
}
MyEditor.js file export MyEditor component used and imported in App.js
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState, RichUtils} from 'draft-js';
import 'draft-js/dist/Draft.css';
import './MyEditor.css'
export default function MyEditor() {
const [editorState, setEditorState] = React.useState(
() => EditorState.createEmpty(),
);
const _onBoldClick = () => {
setEditorState(RichUtils.toggleInlineStyle(editorState, 'BOLD'))
}
return(
<div>
<button onClick={_onBoldClick}>BOLD</button>
<div
>
<Editor
textAlignment="left" placeholder="Enter something here"
editorState={editorState} onChange={setEditorState} />
</div>
</div>
)
}
//ReactDOM.render(<MyEditor />, document.getElementById('container'));
css file for styling
div.DraftEditor-root {
border: 1px solid black;
height: 200px;
width: 200px;
margin-left: 20%;
overflow-y: auto;
}
div.DraftEditor-editorContainer, div.public-DraftEditor-content {
height: 100%;
}

How to apply different background colour and different border colour for reusable components in react?

I am working on react project, In that project I have a Parent component that is App.js and for that Child.js is a Child Component.
Now In App.js, I called two times Child.js. Now I have written some css for Child.js so in output two circles will come.
Now I am trying to achieve different background color and different border color for two circles
This is my code
This is App.js
import React from 'react';
import './App.css';
import Child from './Child/Child';
function App() {
return(
<div className="App">
<Child/>
<Child/>
</div>
);
}
export default App;
This is nothing in App.css
This is Child.js
import React from 'react';
import './Child.css';
function Child({ customstyle }) {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className="exp">
</div>
</div>
</div>
</div >
)
}
export default Child
This is Child.css
.exp {
height: 100px;
width: 100px;
border-radius: 50%;
border: 1px solid black;
}
If I am not clear with my doubt please put a comment
Create two different styles.
.exp {
height: 100px;
width: 100px;
border-radius: 50%;
border: 1px solid black;
}
.exp2 {
height: 200px;
width: 200px;
border-radius: 20%;
border: 1px solid blue;
}
Pass the class name as a prop to Child component and use it in child component for different styling. Like below:
import React from 'react';
import './Child.css';
import classnames from 'classnames';
function Child({ customstyle }) {
return (
<div className='container'>
<div className='row'>
<div className='col-12'>
<div className={classnames(customstyle)}>
</div>
</div>
</div>
</div >
)
}
export default Child

Resources