0

I ma working on a React project, I am trying to define an a state to a button. But state is not applying to a button can you please correct me

This is my code

This is App.js

import React, { useState } from "react";
import { Button } from "antd"
import 'antd/dist/antd.css';
import "./App.css";

const App = () => {
  const [buttonOne, setButtonOne] = useState("red")

  const [buttonTwo, setButtonTwo] = useState({
    backgroundColor: "red",
    color: "black",
    border: "red"
  })


  return (
    <div>
      <Button style={{backgroundColor: buttonOne, border: buttonOne}} className="one" type="primary">First</Button>
      <Button style={{backgroundColor: buttonTwo, color: buttonTwo, border: buttonTwo}} className="two" type="primary">Second</Button>
    </div>
  )
}

export default App
````

2 Answers 2

1

Try this

<div>
   <Button style={{backgroundColor: buttonOne, border: buttonOne}} className="one" type="primary">First</Button>
   <Button style={{backgroundColor: buttonTwo.backgroundColor, color: buttonTwo.color, border: buttonTwo.border}} className="two" type="primary">Second</Button>
</div>

Since your state is object, you need to go inner fields like -

buttonTwo.border or buttonTwo['color']

Example

<Button style={{backgroundColor: buttonTwo.backgroundColor, color: buttonTwo['color'], border: buttonTwo.border}} className="two" type="primary">Second</Button>
Sign up to request clarification or add additional context in comments.

Comments

1

For the second button you would need to access the object's properties.

backgroundColor: buttonTwo.backgroundColor

color: buttonTwo.color

border: buttonTwo.border

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.