Skip to content

Instantly share code, notes, and snippets.

@unicornist
Forked from rhernandog/index.jsx
Last active October 30, 2018 19:25
Show Gist options
  • Save unicornist/0c7324ec7115cf7c3c3e6b24dc95f8e0 to your computer and use it in GitHub Desktop.
Save unicornist/0c7324ec7115cf7c3c3e6b24dc95f8e0 to your computer and use it in GitHub Desktop.
React & Recompose withProps - A simple example of using recompose's withProps
// styles for this are based on Bootstrap 3.3.7
import React, { Component } from "react";
import ReactDOM from "react-dom";
import { withProps } from "recompose";
const users = [
{ "name": "Homer Jay", "status": "pending" },
{ "name": "El Barto", "status": "active" },
{ "name": "SideshowBob", "status": "active" }
];
const User = ({ name, status, button }) =>
<div className={"panel panel-" + (status==="active" ? "primary" : "warning")}>
<div className="panel-body">
This user is {name}
{
button ? (<span> - <button className="btn btn-primary">Click</button></span>) : null
}
</div>
</div>
;
const UserWithButton = withProps({"button":true })(User);
const App = () =>
<div className="container">
<div className="row">
<div className="col-xs-12">
<h2 className="text-center">App</h2>
{ users.map((user) => {
const props = {name: user.name, key: user.name, status: user.status};
return user.status === "active" ? <UserWithButton {...props} /> : <User {...props} />;
}) }
</div>
</div>
</div>
;
ReactDOM.render(
<App />, document.getElementById("app-wrap")
)
@unicornist
Copy link
Author

This gist demonstrates the use-case of HOCs and is a starting point to learn what recompose has to offer.
This gist is a fork, with some minor improvements to the original one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment