All gatsby-image images being rendered as one slide using react-slick - javascript

I have been struggling with this issue for a few weekends now. I'm trying to build a carousel in to my (first) Gatsby website using react-slick however when I view the frontend after running gatsby develop the slider initialised but the markup that gets output is malformed, causing the slider to not work.
I am calling this component on to my index.js page like this, however the rendered output on page looks like this:
<div class="art_list__slider">
<div class="slick-slider slick-initialized">
<div class="slick-list">
<div class="slick-track" style="width: 1348px; opacity: 1; transform: translate3d(0px, 0px, 0px);">
<div aria-hidden="false" class="slick-slide slick-active slick-current" data-index="0" style="outline: none; width: 1348px;" tabindex="-1">
<div>
<div>
<div class="gatsby-image-wrapper" style="position: relative; overflow: hidden; margin: 3rem 0px;">
// image slide here - code removed for space
</div>
</div>
<div>
<div class="gatsby-image-wrapper" style="position: relative; overflow: hidden; margin: 3rem 0px;">
// image slide here - code removed for space
</div>
</div>
<div>
<div class="gatsby-image-wrapper" style="position: relative; overflow: hidden; margin: 3rem 0px;">
// image slide here - code removed for space
</div>
</div>
</div>
</div>
</div>
</div>
</div>
As you can see from the render above, ALL of the individual images are rendering as one slide. How can I fix this?

You need to make sure you import the css as well. Depending on what version of the React Slick you are using you need to include that as well
ie.
npm install slick-carousel
// Import css files
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
As per documentation here
If that is not working for some reason, you can try to reference the css straight from where it is in your node modules file. Alternatively you can try using the cdn solution.
If using React Helmet you can try something like
<Helmet>
<link
rel="stylesheet"
href="href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick-theme.min.css" "
></link>
</Helmet>
Inside the React Component you are rendering your carousel or inside the Layout.js file if you are using this in many places in your project.

As react-slick expects each child node to be it's own slide, you're probably going to have to render your slider in the same component that you make the staticQuery
import React from "react"
import { StaticQuery, graphql } from "gatsby"
import Img from "gatsby-image"
import Slider from "react-slick"
export default props => (
<StaticQuery
query = {graphql`
query {
allFile(
filter: {
extension: { regex: "/(jpg)|(png)|(jpeg)/" }
relativeDirectory: { eq: "gallery" }
}
) {
edges {
node {
base
childImageSharp {
fluid {
...GatsbyImageSharpFluid_withWebp_tracedSVG
}
}
}
}
}
}
`}
render = { data => (
<Slider {...sliderSettings}>
{data.allFile.edges.slice(0, `${props.limit}`).map(image => (
<div key={image.node.childImageSharp.fluid.src}>
<Img fluid={image.node.childImageSharp.fluid} style={{ margin: '3rem 0' }} />
</div>
))}
</Slider>
) }
/>
)

Related

React fill SideNavBar from top to bottom with other components on the page

I want my sidenavbar to continue over the whole page but when i add the other
sections(home, contact & projects) which are just regular functional components
with a div and h1, they create their own space on the page. is there a better
solution to creating different sections on a page? i have tried rendering the components
from index and app.js but without success, i am currently rendering SidenavBar
from index.js and my sections are getting rendered from app.js.
import React from "react";
import "../Section.css";
function HomeSection() {
return (
<div className="Section" id="Home">
HomeSection
<h1>Home</h1>
</div>
);
}
export default HomeSection;
here is an example of the sections, the section.css only centers the text on the page.
function SideNavBar() {
const [titleActive, setTitleActive] = useState(HomeSection);
return (
<div className="SideNavBar">
<Stickybox>
<ul className="SideBarList">
{SideBarInfo.map((info, key) => {
return (
<li
key={key}
className="rad"
onClick={() => {
console.log(info.title + " clicked");
setTitleActive(info.title);
}}
id={titleActive === info.title ? "active" : ""}
>
<Link
onClick={() => {
console.log(info.title + " clicked");
setTitleActive(info.title);
}}
id={titleActive === info.title ? "active" : ""}
activeClass="active"
to={info.title}
spy={true}
smooth={true}
offset={50}
duration={500}
>
<div id="title">{info.title}</div>
/Link>
<Link
activeClass="active"
to={info.title}
spy={true}
smooth={true}
offset={50}
duration={500}
>
<div id="bild">{info.bild}</div>
</Link>
</li>
);
})}
</ul>
</Stickybox>
</div>
);
}
export default SideNavBar;
// this is the css for sidebar
.SideNavBar {
width: 250px;
min-height: 5000px;
background-color: rgb(47, 167, 223);
height: 100vh;
}
this is my sidenavbar component.
My sidenavbar works as intended if i remove the sections, i have tried setting a max width and transparent background for the sections so they dont overwrite the sidenavbar but they still overwrite it. what is the correct way to create different sections?
i added a red background for the home section so its easier to see what its doing.
You probably want to do something like this for your sidebar to fix it on the side and have it take up the full screen.
This will fix the sidebar in a position on the screen (0px away from the top and 0px away from the bottom).
You then define the width and you have a fixed sidebar!
Hope that helps!
.sidebar {
top: 0px;
bottom: 0px;
width: 200px;
position: fixed;
background-color: blue;
}
<div class='sidebar'></div>

Image on React and/or CSS not appearing

I am unable to insert an image to my sign up page. I have tried inserting it using index.js (HTML codes in React component) and also in the corresponding CSS file. Both not working. Sharing the codes and screenshot below (I have removed irrelevant import codes):
React Component code:
import styles from "./register.css";
return(
<div>
<div id="styledImg">
<img alt="" title="" src="../../src/assets/bocLogo.png"/>
</div>
</div>
);
}
export default Register;
CSS code:
#styledImg {
width: 1110px;
height: 428px;
height: 428px;
position: absolute;
top: 245px;
left: 87px;
}
#styledImg Img {
max-width: 100%;
max-height: 100%;
}
You have to import the image and then use the imported file as src.
it would be something like this.
import logo from '.../../src/assets/bocLogo.png';
and then you will use this logo in src
<img src={logo} alt="Logo" />;
for more information please check This link
You have to upload that image as a ReactComponent.
import { ReactComponent as Logo } from "../../assets/your-image.png";
So that should look like:
import styles from "./register.css";
import { ReactComponent as Image } from "../../assets/your-image.png"
return(
<div>
<div id="styledImg">
<img alt="" title="" src={Image}/>
</div>
</div>
);
}
export default Register;

Placing components on the right and around other items in React problem

Sorry for my atrociously bad title on this post.
I would like to achieve something like this as my end goal (my figma mockup):
But I don't know how to achieve this as when I call a component on a "page", it just places the items underneath the navbar like this:
(the text like iPad momentos was just for me testing if the components worked and were called correctly)
I haven't developed all of the components you can see in the mockup (figma), but that shouldn't matter. All I would like to know is with (S)CSS, how you can place different flexboxes or items around each other like in the image below.
Thanks a lot.
My code:
// This file is SRDash (you could call it a page if you want)
// Normal Imports
import React from 'react';
import CardLink from '../components/CardLink';
// Component Imports
import SNavbar from '../components/SNavbar'
import Text from '../components/Text'
// Styling Imports (Global Styles for this Dash)
import './sradmin.scss'
const SrDash = () => {
return (
<div className="mainContent">
<SNavbar />
<Text />
<CardLink text="text here" link="asdfsd" svg="asdfsdf"/>
</div>
);
}
export default SrDash;
It's styling:
#import url('https://fonts.googleapis.com/css2?family=Poppins:wght#300&display=swap');
* {
margin: 0;
}
body {
color: blue;
}
Navbar SCSS:
body {
margin: 0
}
.snavbarcontainer {
background-color: black;
min-width: 3.5em;
max-width: 3.5em;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.icons_ul {
text-decoration: none;
list-style-type: none;
padding: 0;
}
.icons_ul li {
margin: 0.5em 0;
}
.icons_ul {
justify-content: center;
}
.toplefticon {
position: absolute;
top: 0;
transform: translateY(50%);
}
Navbar JSX:
import React from 'react';
import './navbar.scss';
// SVG Imports
import topleft from '../assets/toplefticon.svg'
import IDash from '../assets/dashboard.svg'
import IDB from '../assets/database.svg'
import IHand from '../assets/hand.svg'
import ILookupPupils from '../assets/lookup-pupils.svg'
import IMAdmins from '../assets/manage-admins.svg'
import IMUsers from '../assets/manage-users.svg'
const SNavbar = () => {
{/* <img className="testimage" src={topleft} alt="Testing Logo" /> */}
return (
<div className="snavbarcontainer">
<div className="toplefticon">
<a className="test" href="#"><img src={topleft} alt="Testing Logo" /></a>
</div>
<div className="mainIcons">
<ul className="icons_ul">
<li><img src={ILookupPupils} alt="Pupil Lookup" /></li>
<li><img src={IMUsers} alt="Manage Users" /></li>
<li><img src={IHand} alt="Coming Soon" /></li>
<li><img src={IMAdmins} alt="Manage Admins" /></li>
<li><img src={IDash} alt="Dashboard" /></li>
<li><img src={IDB} alt="Dashboard" /></li>
</ul>
</div>
</div>
);
}
export default SNavbar;
Thanks again.
Give mainContent css with property
display:flex;
Hey so after hours of trying to figure it out, the solution was to seperate the navbar and the actual content into seperate divs, like below:
<div className="mainContent">
<SNavbar />
<div className="content">
<Text />
<CardLink text="text here" link="asdfsd" svg="asdfsdf"/>
</div>
</div>
This worked perfectly and after which I had no issues with, and look at the outcome now!

How to create virtual scrolling for images using React js

Whats my requirement: i have some images in my external folder and i need to import to component and display it and also have to use Virtual Scroll here i have to display 1 row in div and in that 1 row have to show 5-6 images
What i did : i consumed images using context from external folder and showed images in 1 rows in div and 5-6 images but i am facing issue unable to set it to Virtual scrolling
as i checked react-virtualized & react-window plugin but i am not sure how my data is used in that format
After using the react-tiny-virtual-list images are getting stacked
below is my code
class App extends React.Component{
state={
Response:[],
}
importAll(r) {
return r.keys().map(r);
}
componentWillMount() {
let data = this.importAll(require.context('./imageFolder/', false, /\.(png|jpe?g|svg)$/));
this.setState({ Response:data})
}
render(){
return(
<div className="container" id="imagecontainer">
<div className="viewport">
{this.state.Response.map((image, index) => <img key={index} src={image} alt="info"></img> )} }
</div>
</div>
)
}
.container {
padding: 0% 6%;
height: 400px;
}
.viewport {
height: -webkit-fill-available;
width: 100%;
border: 1px solid black;
overflow: scroll;
}
img {
height: 250px;
width: 150px;
padding: 35px;
}
After implementing React-tiny-list
<div id="container">
<div id="viewport">
<VirtualList
height='400px'
width='100%'
itemCount={this.state.items.length}
itemSize={20} // Also supports variable heights (array or function getter)
style={{padding:'20px'}}
renderItem={({index, style}) =>
<div key={index} style={style}>
<img key={index} src={this.state.items[index]} alt="info"></img>
</div>
}
/>
</div>
</div>
you can also use the https://github.com/bvaughn/react-virtualized plugin in this if you want to display this as table you can choose list or you can choose grid also .For you requirement i recommend using Masonry from 'react-virtualized';
below is the sample for displaying
import React from 'react';
import {
CellMeasurer,
CellMeasurerCache,
createMasonryCellPositioner,
Masonry
} from 'react-virtualized';
import 'react-virtualized/styles.css';
var images = [];
const columnWidth = 250;
const defaultHeight = 260;
const defaultWidth = columnWidth;
const cache = new CellMeasurerCache({
defaultHeight,
defaultWidth,
fixedWidth: true
})
// Our masonry layout will use 3 columns with a 10px gutter between
const cellPositioner = createMasonryCellPositioner({
cellMeasurerCache: cache,
columnCount: 4,
columnWidth,
spacer: 27
})
function cellRenderer ({ index, key, parent, style }) {
const datum = images[index]
const height = columnWidth || defaultHeight ;
return (
<CellMeasurer
cache={cache}
index={index}
key={key}
parent={parent}
>
<div style={style}>
<img
src={datum}
style={{
height: height,
width: columnWidth,
display: "block"
}}
alt="info"
/>
</div>
</CellMeasurer>
)
}
class Grid extends React.Component{
importAll(r) {
return r.keys().map(r);
}
componentWillMount() {
images = this.importAll(require.context('../imageFolder/', false, /\.(png|jpe?g|svg)$/));
}
render(){
return(
<div id="container">
<div id="viewport">
<Masonry
cellCount={images.length}
cellMeasurerCache={cache}
cellPositioner={cellPositioner}
cellRenderer={cellRenderer}
height={400}
width={1320}
/>
</div>
</div>
)
}
}
export default Grid;
I hope this will resolve your issue
If you're having trouble implementing the virtual scroll, note that the order of the imports is important when doing this, so pay heed to this - it could be contributing to your issue. (An aside: There is an npm plugin for implementing a virtual list.)
An overview of the import order for virtual scroll is:
import * as React from 'react';
import Paper from '#material-ui/core/Paper';
import {
Grid,
VirtualTable,
TableHeaderRow,
} [from material ui];
import {
your-components
} from 'your-path';
(above is non-specific, just a rough guide to the order)
You could also use a ScrollView if you are unable to implement a "Virtual scroll".
The following style will give you a horizontal scroll (as opposed to the default vertical), to enable you to display your images in a horizontally-scrollable row
<ScrollView horizontal={true}>
Hope this helps

Adding inline styles to react

Currently I'm using react.js and I'm trying to get two div's to be side by side.
Currently I'm trying to
<div id="sidebar" style = "display:inline-block;" >
<script src="build/sidebar.js"></script>
</div>
<div id="map-canvas" style="display: inline-block; width: 20%; height: 50%; "></div>
with sidebar.js as the where react is stored. This unfortunately doesnt work however as it just moves map-canvas to the side while sidebar isn't to the left of it; its on top. I've tried many various combinations w/ float as well and none of them seem to work
Another option is to edit the sidebar.js code where I currently have
return <div>
<input type="text" value={this.state.searchString} onChange={this.handleChange} placeholder="Type here" />
<ul>
{ libraries.map(function(l){
return <li>{l.name} </li>
}) }
</ul>
</div>;
in which I try doing return <div style ="display: inline-block;">
However, in this case the generated html doesnt show up at all. I'm perplex to what I should try but react seems like it doesnt want to play nice with other div elements.
That's because in React, the style prop takes an object instead of a semicolon-separated string.
<div id="sidebar" style={{display : 'inline-block'}} >
<script src="build/sidebar.js"></script>
</div>
<div id="map-canvas" style={{display: 'inline-block', width: '20%', height: '50%'}}>
</div>
Edit:
So this:
return <div style="display: inline-block;">
Would become this:
return <div style={{display: 'inline-block'}}>
const styles = {
container: {
backgroundColor: '#ff9900',
...
...
textAlign: 'center'
}
}
export default class App extends React.Component {
render() {
return(
<div className="container" style={styles.container}>
// your other codes here...
</div>
);
}
}

Categories

Resources