Skip to content

Example needed: Change value of some prop for all uses of some component #3

Description

@tylercollier

Say I have ComponentA and ComponentB. ComponentA will render() some arbitrary element tree that includes B at arbitrary locations. I want to change all of the myValue props on ComponentB to be prefixed with the value '_'. How can I do this?

I want this:

class ComponentA extends React.Component {
  render() {
    return (
      <div>
        <ComponentB myValue={something} />
        <div><ComponentB myValue={something2} />
      </div>
    )
  }
}

to become this:

class ComponentA extends React.Component {
  render() {
    return (
      <div>
        <ComponentB myValue={'_' + something} />
        <div><ComponentB myValue={'_' + something2} />
      </div>
    )
  }
}

Also, the replaceDivsWithSpans example from the README uses DOMElement in the visitor, but what would I use for a component (not a component element)? Using ComponentElement works for e.g. transformComponents(wrapRender(transformNode))(<ComponentA/>) but not transformComponents(wrapRender(transformNode))(ComponentA). No method on my visitor gets called (e.g. ComponentElement). I'm not sure if this is because I'm using React 15.3?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions