React: bump to 0.5.1, fix bugs, improve perf, improve style

This is based on #693 and supersedes it. It also bumps the React version and fixes the issues discussed in the review. We should be good to go with this.