GithubHelp home page GithubHelp logo

Comments (8)

ritikbanger avatar ritikbanger commented on July 17, 2024 3

Please close the issue, if the problem is solved. @arunkumar413

from react.

arunkumar413 avatar arunkumar413 commented on July 17, 2024 2

Thanks all for the answers. The spread operator will suffice my requirement.

from react.

Hudsxn avatar Hudsxn commented on July 17, 2024 1

Yeah, this is simple enough to do with a spread operator. You can prepare your props dynamically before and use the ... operator:

const inputAttributes = {
    type: 'text',
    name: 'firstName'
};
return <input {...inputAttributes}/>;

from react.

Hudsxn avatar Hudsxn commented on July 17, 2024 1

No problem :)

A good understanding of JavaScript takes time. For extra information, you can use the spread operator on both object and arrays, as such:

const mainObject = {
    hello: true
};
const secondObject = {
    ...mainObject,
    world: false
};
console.log(secondObject) // outputs { hello: true, world: false }

And with arrays

const arrayOne = [1,2,3];
const arrayTwo = [...arrayOne, 4,5,6];

console.log(arrayTwo); // outputs [1,2,3,4,5,6]

from react.

ritikbanger avatar ritikbanger commented on July 17, 2024

Could you please list the benefits out for doing so? The input tag is a native HTML tag. If you want, you can still create a parent component that may take a single object prop and then you pass them to the underlying input tag.

from react.

prashanth-cn avatar prashanth-cn commented on July 17, 2024

we can spread the props as well

from react.

pwbriggs avatar pwbriggs commented on July 17, 2024

Elaborating on @prashanth-cn's comment and the React docs, this is already possible using JavaScript's spread syntax. Instead of the normal <Element key={value} /> syntax, you can spread an object inside a pair of curly braces.

So this code:

function App() {
  return <Greeter salutation="hello" name="world" />
}

is equivalent to

function App() {
  const config = {salutation: "hello", name: "world"}
  return <Greeter {...config} />
}

from react.

pwbriggs avatar pwbriggs commented on July 17, 2024

Sorry, I didn't see @Hudsxn's reply.

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.