Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

how can I use a state as an array of objects

$
0
0

I have recently started learning React TypeScript, and I am trying to make a Bowling Score application. I thought about state (I don't know much about states, just started learning it) so I can put an array of object inside the state and use it which will make the values in each frame changes when I press the roll buttons. This is my code :

  type frame = {    leftScore: any;    rightScore: any;    lastFrameScore: any;    totalScore: any;}const game = new Game();const [score , setScore] = useState<frame[]>([]);function App() {    setScore({score : game.score}); // got error here    return (<div className='container'><div className="RollsContainer"><h1>hi </h1>                {[...Array(11)].map((e , i) =><button key={i} onClick={() => handleRolls(i)}>{i}</button>                )                }</div><div className="frameContainer">                {[...Array(10)].map((e , i) =><Frame                        framenbr={i + 1}                        leftbox={score[i + 1].leftScore}                        rightbox={game.score[i].rightScore}                        lastbox={game.score[i].lastFrameScore}                        totalScore={game.score[i].totalScore}                        key={i + 1}/>)                }</div></div>    )}

this is game class:

export class Game {    curentFrame: number;    roll: any;    score: frame[] = [];    constructor() {        this.curentFrame = 0;        for (let i = 0; i <= 10; i++) {            this.score[i] = {                leftScore : '' ,                rightScore : '' ,                lastFrameScore : '' ,                totalScore : '' ,            };        }    }}

I got this error

Error:(15, 15) TS2345: Argument of type '{ score: frame[]; }' is not assignable to parameter of type 'SetStateAction<frame[]>'.  Object literal may only specify known properties, and 'score' does not exist in type 'SetStateAction<frame[]>'.

I don't know if this is right or wrong I tried so many solutions but did not work


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>