score:1
Option 1:-
const [myStyle, setStyle] = useState({border: '2px solid red', color: 'yellow'})
<div style={myStyle}>Hello First Trial</div></div>
Option 2:-
const [myStyle, setStyle] = useState([
{name: 'border', value:'2px solid red'},
{name: 'color', value:'yellow'}
])
Use reduce method to prepare a single object out of the array.
<div
style={myStyle?.reduce((agg, val) => {
agg[val.name] = val.value;
return agg;
}, {})}
>
Hello Second Trial
</div>
Codesandbox - https://codesandbox.io/s/epic-wildflower-sutkr?file=/src/App.js:294-495
score:2
use this one it works fine
const [myStyle, setStyle] = useState({border: '2px solid red', color: 'yellow'})
and
<div style={myStyle}>Hello First Trial</div></div>
Source: stackoverflow.com
Related Query
- How to keep and loop the style of HTML element from an Array in REACT?
- React : how to pass and array from inside a Function to the return (JSX)
- How to remove selected index of an array from the DOM and reflect the change in React state?
- Access and change the state array element in main React App from grandchild
- How do I iterate over an array of objects match a common element from another array and return the key value for "name"
- React Native - How to change the style and properties to a pressed row inside a map loop
- How to write test for the HTML element being passed as a prop to a component a and a function is being called from that
- how do i pass array from select html element component to two text components that have different values in the array
- how can I get the first unique ID element and last unique ID element in an array React js
- How do I target and change the style of a different element when I click a button in react
- how to get the rendered HTML element from the screen, from react testing library?
- React event target value from jsx element does not get bind to state and json array (Type error cannot read the properties of undefined )
- how to loop throgh array of object from axios json respone in react js and set them in div?
- How to use filter from an array while fetching graphql query and also must keep the array order?
- How to save the input from user given using html input tag to a array in "state" in React JS
- how can i loop through the array inside of an object. and render it in the react component
- how to append react div element from an array with a child element ? and how to give to mapped parent elements unique "key" props
- How to display the image and background image from the array in react
- How to take items from an array that is in a React component's props and render to the page?
- How to add objects retrieved from fetch request to the state array in React and then update the view?
- How to loop through an array and display data within the object of that array, in react and javascript
- Getting data from a JSON array and setting states based on the data using loop in React
- How to send array of objects data from child component to parent component and store in the parent object using react hooks?
- How to use for loop in react so I can loop over two array and return the object in second array if both match
- Making an HTML string from a React component in background, how to use the string by dangerouslySetInnerHTML in another React component
- How to slide <View/> in and out from the bottom in React Native?
- CDN links for React packages and how to import it when using react using the scripts from CDN
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How to activate a react route and pass data from the service worker?
- How to open a text file and read the contents from it on react native?
More Query from same tag
- How to edit the rerender item in react hooks
- Updating an array with useState in a form
- How to find out when a youtube embedded iframe is loaded
- ReactJs Checkbox value and date not getting updated in state
- Filtering data and returning it next to the "permanent" data - react
- Apply changing markers array to Google map in react
- How do I import ES6 Module within a create-react-app without causing tests to fail?
- React how to convert string to object property
- Populate React Table with firebase realtime database data
- Don't output same items from array
- Update React State dependent from another state with hooks
- TypeError: Cannot read property 'map' of undefined in REACT project?
- Serving react over static server without having people running into 404
- Issue with PUT when using custom id in JSON server
- Component crashes when re-render
- How to properly call the callback in setState(updater[, callback])?
- How to inject react element via chrome extension?
- Detect broken image when using hydrate instead of render from react-dom
- How to get value of textbox in React?
- ComponentDidMount does not work - props do not pass to child component
- Passing array to props but getting error while iterating
- TypeError: evt.target is undefined
- Adding Foreign Objects as React Components in a D3 svg node
- Confused on service worker with NextJS & Express
- React Typescript: Problem when i try to send history argument from a component to an action using withRouter
- White flash while navigation bar moves in and out of view
- Optimistic Updates Using Flux (async)
- How to send data to database with user account from Firebase?
- Filtering with checkboxes with useState
- Change props from imported component