Blueprint: Use React.PureComponent instead of pure-render-decorator

Created on 14 Nov 2016  路  4Comments  路  Source: palantir/blueprint

docs: https://facebook.github.io/react/docs/react-api.html#react.purecomponent

  • it is just as usable as the decorator; you can override shouldComponentUpdate to opt out of pure rendering
  • this reduces complexity by removing 2 dependencies (decorator + its typings)
  • this will require rewriting the tslint rule which enforces pure components this seems to have gotten lost somewhere in the last few months...
  • :fire: requires a react dependency of >= 15.3.0
P2 breaking change refactor help wanted

Most helpful comment

just in case it helps, here's the ruby script I used to make this change in our codebase. (Note that it isn't designed to be robust, it might not work in cases with multiple decorators, etc.) You want to run it in your root src directory.

files = Dir['**/*.{ts,tsx}']
files.each do |f|
    lines = File.readlines(f)
    lines.delete_if { |line| line =~ /import|require/ && line =~ /pure-render-decorator/ }
    # remove line with decorator, change if you use a different import name
    while decorator_index = lines.find_index { |line| line =~ /@PureRender/ }
        lines[decorator_index + 1].sub!(/Component</, 'PureComponent<')
        lines.delete_at(decorator_index)
    end
    File.write(f, lines.join(''))
end

Also, doing this means the peerDependencies on React will have to be bumped to ^15.3.0 or something similar. It might be prudent to hold off for a little on doing this to not force people to upgrade React

All 4 comments

just in case it helps, here's the ruby script I used to make this change in our codebase. (Note that it isn't designed to be robust, it might not work in cases with multiple decorators, etc.) You want to run it in your root src directory.

files = Dir['**/*.{ts,tsx}']
files.each do |f|
    lines = File.readlines(f)
    lines.delete_if { |line| line =~ /import|require/ && line =~ /pure-render-decorator/ }
    # remove line with decorator, change if you use a different import name
    while decorator_index = lines.find_index { |line| line =~ /@PureRender/ }
        lines[decorator_index + 1].sub!(/Component</, 'PureComponent<')
        lines.delete_at(decorator_index)
    end
    File.write(f, lines.join(''))
end

Also, doing this means the peerDependencies on React will have to be bumped to ^15.3.0 or something similar. It might be prudent to hold off for a little on doing this to not force people to upgrade React

馃敟 breaking change because it requires React >= 15.3!

In package.json react requirement is already >= 15.5.1, is this still beraking change?

@betalb no, the peerDependency still includes react v0.14 and will stay that way until blueprint core 2.0: https://github.com/palantir/blueprint/blob/3782d6d2ac5c0eacd21bfa2d6a03c75dd9e6d804/packages/core/package.json#L20

Was this page helpful?
0 / 5 - 0 ratings