score:0
You could try to shuffle your array like this:
function shuffleArray(array) {
return array.sort(function () {
return Math.random() - 0.5;
});
}
EDIT: You also should create a new object/array after shuffling. Otherwise React will not detect any change.
EDIT 2: Slightly improved implementation of your shuffle function:
function shuffleArray(array) {
const newArray = [ ...array ];
for (let i = newArray.length - 1; i > 0; --i) {
const j = Math.floor(Math.random() * i);
const temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
}
return newArray;
}
score:0
I have cleaned up my code some, particularly the useEffect. I'll post below. The problem was simpler than I realized. If your array.map has a parent div with the proper key, it is redundant to provide it for the children. Doing so brings a warning because it's interpreted as duplicate keys, not as children of the parent (which I incorrectly believed was required to collect the proper info for the children from the parent). I do suggest some deliberation over shuffling vs. randomizing for similar projects. Randomizing is bound to have repeats
import {scrollImages} from '../../Data/data'
import {scrollTestimonials} from '../../Data/data'
const HomeScroll = () => {
function shuffleArray(array) {
var i, j, temp;
for (i = array.length -1; i > 0; i--) {
j = Math.floor(Math.random() * i);
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
const [list, setList] = useState([])
const [id, setId] = useState(0)
const delay = 10000;
useEffect(() => {
const mountArray = shuffleArray([...scrollTestimonials])
setList(mountArray)
setTimeout(() =>
setId((prevIndex) =>
prevIndex === scrollImages.length - 1 ? 0 : prevIndex + 1
),
delay
);
return () => {};
}, [id]);
return (
<div className="wrapper">
<div className="slider">
<div className="slides"
style={{ transform: `translate3d(${-id * 2.5}%, 0, 0)` }}
>
{scrollImages?.map((item) => (
<div className="widthWide" key={item.id}>
<img className="slide" src={item.img} alt="Slide" />
</div>
))}
<div className="wrapperTest">
<div className="sliderTest">
<div className="slidesTest">
{list?.map((x, index) => (
<div className="slideWrapper" key={x.id + index + x.name} >
<div className="slideContent" >
<div className="testimoneyWrapper" >
<FontAwesomeIcon icon={faQuoteLeft} className="leftQuote" />
<div className="slideTestimoneyWrapper">
<p className="slideTestimoney">{x.text}</p>
</div>
<FontAwesomeIcon icon={faQuoteRight} className="rightQuote" />
</div>
<div className="slideInfos" >
<p className="slideName">{x.name}</p>
<div className="slideInfo">
{x.site === "Yelp" ?
<>
<FontAwesomeIcon icon={faYelp} size="2x" color="#0099CC" />
<p className="slideInfo1">Yelp</p>
</>
:
<>
<FontAwesomeIcon icon={faGoogle} size="1x" color="#EA4335" />
<p className="slideInfo2">oogle</p>
</>
}
</div>
</div>
</div>
</div>
))}
</div>
</div>
</div>
</div>
</div>
</div>
)
}
export default HomeScroll
score:1
in JS, you could use the library of Lodash, that has a function .shuffle(collection)
. Then use the shuffled data as model of your view.
Source: stackoverflow.com
Related Query
- How can I shuffle an array of objects on page render and map over the shuffled array without console warning of "two children with the same key"?
- How do I map over an array of objects in React and then conditionally render a component based on a previous value?
- How can I filter and map over an array at the same time?
- How can I drag and drop objects into a Mapbox map from outside the map?
- How can I use Promise.all on an array of objects and then reassign them to the relevant key in a new object?
- how to groupby objects using lodash and map over the list -react
- How to map over 2 Array of object in React and render to Table
- How do I get the "results" object nested in the array "news" and map over it?
- cant map over and fetch the data present inside an array of objects
- How do I iterate though an array of objects with keys and values in react and render them in the JSX( Both keys and Values)
- how to map over an Object and render the arrays inside the object javascript/reactjs?
- In React How can I fetch data and render a single object in a states array without mapping the whole thing?
- How do I map thorough the entire array which contains other objects and arrays as well in React?
- How can I compare two arrays of objects and add new key to an objects in the second array
- How can I extract a nested array of objects and put them in the core array?
- How do I iterate over an array of objects match a common element from another array and return the key value for "name"
- How to only render 25 objects to the screen at a time even though I am mapping over an array containing 1,000 objects using React.js
- how can I put a logic in map array method. I have sent title as props and except props title I want to render other title
- How to map over 2 Array in React and render to Table
- how can i loop through the array inside of an object. and render it in the react component
- How to map over array of dissimilar objects and return JSX without Error
- How can I access two separate images in an array of objects and assign it to a third object inside the same array?
- How can I set a specific position to array elements with the map function and a given index?
- How can I map over my data set and set a single variable to an objects name?
- How can I map array with multiple objects and display on chart?
- How to fetch an array of objects and render it in the component as a normal text using React Hooks?
- Is there a way to map over an array of objects in react and also get the accosiated images from cloudinary
- How to use for loop in react so I can loop over two array and return the object in second array if both match
- How to loop and render elements in React.js without an array of objects to map?
- How can I map over two arrays at the same time?
More Query from same tag
- Polaris navigation onclick method
- CSS - drawing lines between dynamically sized boxes
- React equivalent to ejs
- Remove object property based on array values
- How to Disable Row in Antd Table
- computed style in setState of react doesn't work
- How to change SIZE of bootstrap button according to value in REACT
- DOM does not update on value change
- How do I use React setState() to set the state to a specific value that is a constant?
- Difference between <function prop="Hello"> and {function("Hello")}
- 304 and no json response on deployed app on Heroku
- Does graphql support filters on aggregates?
- How do you transition framer motion values?
- Cloudinary responsive images only displaying on window size change in React using Webpack
- How to get initialValues for a reduxFrom from state?
- Trying to load multiple C3 charts in same react component
- Can I use regular bootstrap toast in reactjs?
- Meteor subscription ready
- Can I import a handleChange function and bind it for use in any component?
- Socket.io updating multiple times
- integers inside JSX curly braces
- How do i link Goods List & Goods in shopping page with React
- Assigning Object in function to Variable or call setState w/o this?
- Module not found: Can't resolve 'swiper/css'
- Passport.js with openid-client strategy-- TypeError: client must be an instance of openid-client Client
- How to get music's info from input type file in ReactJs
- Find best Route and Price in an array
- Stop watching folder changes in reactjs
- Calling axios request one after the other?
- Cannot highlight the nav menu on current page in Antd menu component