flopic
flopic

Reputation: 1109

Cannot use map for getting useState data

I'm trying to print my state but I cannot access it with a map because it's not an Array

I create like this :

const [state, setState] = React.useState({
    checkedA: "",
    checkedB: ""})

When I console.log my state it render :

state: {checkedA: "test", checkedB: "test2"}

I try to create an Array and split(',') but same error

"state.split is not a function"

Any idea how to access to it?

Upvotes: 0

Views: 80

Answers (1)

Cat_Enthusiast
Cat_Enthusiast

Reputation: 15688

You can use Object.values() to take the values of your object and put them in an array. Then use .join() to turn them into a single string.

state: {checkedA: "test", checkedB: "test2"}

Object.values(state).join("") <-- returns "testtest2"

Upvotes: 1

Related Questions