score:0
Make your state an array of colors like
this.state = {
colors: [],
}
Then generate and push n
randoms colors into it, iterate over the array and render the children
It will look something like this:
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
colors: []
};
this.setRandomColors = this.setRandomColors.bind(this);
}
generateRandomColor() {
let randomColor = "rgb(";
for (let i = 0; i < 3; i++) {
randomColor += Math.floor(Math.random() * 255);
if (i < 2) {
randomColor += ",";
}
}
randomColor += ")";
return randomColor;
}
setRandomColors() {
const colors = [];
for (let i = 0; i < 3; i++) {
colors.push(this.generateRandomColor());
}
this.setState(() => { return {colors: colors}; });
}
render() {
return (
<>
{this.state.colors.map((color) => {
return <ColorDiv color={color}></ColorDiv>;
})}
<button onClick={this.setRandomColors}>Press me!</button>
</>
);
}
}
Source: stackoverflow.com
Related Query
- How can i pass different props to more than one children component from the parent one in React?
- How can I display more than one component from the same file in the App.js?
- How can i import more than one component from the same folder?
- Can you use this.props.history.push to pass props to a different component than the pathname says?
- What are the different types of props can be send from one component to the other using properties in React?
- How can I get more than one value from the Redux store in a single statement?
- How can I pass the state from one component to another and how can I modify the state?
- How can I pass props from one component to another using Link and router in react
- React - How can I pass API data from one component to another in a different js file?
- How can I update the state of my container component from one of its children in React?
- How can I pass props from one component to another withour Redux
- How can I modify the props of one child component from a separate child component?
- How can i pass props from a functional button component to another function component that displays the prop?
- How can I correctly pass state as props from one component to another?
- how can I choose to send pass different props to a react component according to the current viewport size?
- How can i extract more than one key from an Object and print the output
- Can we pass setState as props from one component to other and change parent state from child component in React?
- How to pass function as props from one functional component to another?
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How do you set the state of one component from a different component in react?
- How to pass route parameters from one Component and display in the other
- How to pass the variable from one component to another
- Redux: How to pass props from one component to another?
- How can I pass the children of a function component to another function component in a react class?
- How to pass props from one page using a Link(react-router) to a class component
- How can I pass props to components from different sources?
- How can I pass image url from parent component to child component via props [REACT.JS]
- Async props pass in React: How to pass a key to update redux store in react? [First from the higher component and then from redux store]
- How to pass props from html div to the function component in react?
- How can I get more than one document from mongoDB?
More Query from same tag
- Pause video when changing slide in react js?
- React get component size
- Can't understand this behaviour
- useRef current and click event target are not equal even though they should be
- Enable certain dates in a datepicker
- React Pluggable - Component isn't removed from the DOM on uninstallation of a plugin
- Accessing align environment in ReactMarkdown
- Is there anyway to bring up the color picker in VSCode when writing JS/JSX files?
- React re-execute inner useState function after each re-render
- Re-routing to index after submit React/Redux
- How to compare css width of two divs in React JS using Cypress?
- How to convert String into a HTML element in Next JS Applcation?
- Page wont refresh when using React Link and passing state
- I want to copy a link which is stored in a variable in React
- Changing two states sequentially throws an error
- MUI Autocomplete filterOptioms still displaying all options
- Reactjs react-day-picker on Safari in iOS
- Uploading image to firebase storage: Unhandled Rejection (TypeError): Cannot read property 'state' of undefined
- How can I set a userId inside the users object when an icon is clicked
- Adding new value to CreatableSelect onClick in button for React-Select
- Should I be using Redux Sagas?
- Getting an "Exported queries are only executed for Page components." in Gatsby when trying to generate pages
- React router Link wrong url
- Setting react-router link prop with a condition statement returns a syntax error
- There is TypeError (0 , _ColorReducer.color) is not a function error in React + Redux code
- Why it is continuously re-fetching graphQL on every second?
- ENOENT error when serving React App on Azure
- Is there a way to include a conditional inside of a state declaration?
- How to push inside nested array of object that have a precise value of a key in MongoDB?
- ReactJs + Electron - Socket connection only working after reload