-
-
Save unicornist/0c7324ec7115cf7c3c3e6b24dc95f8e0 to your computer and use it in GitHub Desktop.
React & Recompose withProps - A simple example of using recompose's withProps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.