score:2
Accepted answer
Are you looking for something like this? Check my demo below.
import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
class App extends Component {
constructor() {
super();
this.state = {
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'],
letters: ['', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz', '', '', ''],
numberClicked: [],
addClicked: []
};
}
componentWillMount()
{
const v = [];
for (var i = 0; i < 12; i++)
v.push("notClicked")
this.setState({
addClicked: v
})
//console.log(v)
}
update = (number) => {
const n = [...this.state.numberClicked]
const c = [...this.state.addClicked]
c[number - 1] = 'clicked'
console.log(c)
n.push(number)
this.setState({
numberClicked: n,
addClicked: c
}, () => {
let a = "";
for (var i in this.state.numberClicked)
{
a = a + this.state.numberClicked[i] + " "
}
console.log("All Values: ", a)
})
}
render() {
return (
<Numbers numbers={this.state.numbers} letters={this.state.letters} update={this.update}/>
);
}
}
class Numbers extends Component {
constructor(props) {
super(props)
}
render() {
return (
<div className="parent-wrapper">
<div className="parent">
<div className="child" onClick={this.props.update.bind(this, "1")}><h1>{this.props.numbers[0]}</h1><p>{this.props.letters[0]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "2")}><h1>{this.props.numbers[1]}</h1><p style={{marginLeft: "16px"}}>{this.props.letters[1]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "3")}><h1>{this.props.numbers[2]}</h1><p style={{marginLeft: "17px"}}>{this.props.letters[2]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "4")}><h1>{this.props.numbers[3]}</h1><p style={{marginLeft: "17px"}}>{this.props.letters[3]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "5")}><h1>{this.props.numbers[4]}</h1><p style={{marginLeft: "20px"}}>{this.props.letters[4]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "6")}><h1>{this.props.numbers[5]}</h1><p style={{marginLeft: "14px"}}>{this.props.letters[5]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "7")}><h1>{this.props.numbers[6]}</h1><p style={{marginLeft: "13px"}}>{this.props.letters[6]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "8")}><h1>{this.props.numbers[7]}</h1><p style={{marginLeft: "17px"}}>{this.props.letters[7]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "9")}><h1>{this.props.numbers[8]}</h1><p style={{marginLeft: "12px"}}>{this.props.letters[8]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "10")}><h1>{this.props.numbers[9]}</h1><p>{this.props.letters[9]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "11")}><h1>{this.props.numbers[10]}</h1><p>{this.props.letters[10]}</p></div>
<div className="child" onClick={this.props.update.bind(this, "12")}><h1>{this.props.numbers[11]}</h1><p>{this.props.letters[11]}</p></div>
</div>
</div>
);
}
}
render(<App />, document.getElementById('root'));
Source: stackoverflow.com
Related Query
- How to make a loop to reuse a function
- Does it matter if the function calls in this loop run concurrently? And if so, how do I make each wait until previous is done?
- How to fix jslint error 'Don't make functions within a loop.' while using find function inside loop
- How to loop inside map function using jsx format React JS
- How to make for loop on react-select?
- How to make key value of map function increment for each element in JavaScript
- How to make a function excute before an other function
- How to make this available inside render function
- How to call a function after all async calls in loop are done?
- How to make this JSX code more generic to avoid code reuse in React?
- How would you make an async/await function to show a dialog in React?
- How to make "dispatch" as export function in React JS?
- How to make a loop request with React using Axios?
- How can I make tx.executeSql() function sync with async/await?
- How to make common API call function using fetch
- How to make a similar function as componentDidUpdate function in a functional component in React?
- How can I use a loop to make this React component more DRY?
- How to make carousel auto change instead of click onclick with loop on ReactJs
- How to make sure the function "window.location.reload()" only fires once instead of having an infinite loop?
- How to make my function more maintainable with Object.entries method in React
- How to make onClick function fire on the click of an icon?
- In Reactjs, how to make a prop function do separate tasks and return their values according to the required condition
- How to make a function as pure function using javascript and react?
- How to make my internal callback function reactive for mobx state change?
- How to make a use of arrow function in es6 instead of bind method
- how to make a recursive function asynchronous
- How can I make this function shorter with ES6?
- How to make 3 column grid with map function react
- How to make a props function optional
- How can I make a function Async in reactjs which calls another two functions from redux actions
More Query from same tag
- How do I customize variables to prevent overwriting styles?
- Populate dynamically rendered form fields in react
- Limiting single button clicks in an environment using JavaScript objects and React
- Table Elements are equal. but still returns error
- React - iterating over key value pairs in array
- Django, Djoser social auth : State could not be found in server-side session data. status_code 400
- "You have exceeded your request quota for this API" in Google Map
- Can not export webpack.config when modules:false
- How can I remove the part below the carousel?
- React Hooks - Context
- "Yup" Validation Library fails to compile in TypeScript
- How do I implement protected client routes?
- In React, how to get the ID of a dropdown when the dropdown value changes?
- React fetch failed: "TypeError: Failed to fetch" even though the API returns data
- React js - Add unique key onClick
- How to use react-bootstrap in typescript project
- Align typography text to center
- Using one instead of multiple makeStyles hooks
- want to insert data in table in reactjs
- Markdown component not rendering from content stored in State
- parsing markup in react-mentions package
- How to make my function more maintainable with Object.entries method in React
- Generic typescript component inherit component props
- How to render images from an array using the map() method?
- How to use separation of concern with react-query (in a clean architecture context)
- Why ReactJS components must act like pure functions?
- Semantic UI React Sidebar OnHide
- DraftJS editor create from HTML is not working
- How can I render my component X amount of times based on a selector?
- import a Json file in a React Component