Last active
December 17, 2020 14:53
-
-
Save rhernandog/5bcc68558b62cf034aada402a5161980 to your computer and use it in GitHub Desktop.
React & Recompose withProps - A simple example of using recompose's withProps
This file contains 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 Another = withProps({"button":true })(User); | |
const renderUsers = (e,i) => { | |
const props = {name:e.name, key:e.name, status:e.status}; | |
return e.status === "active" ? <Another {...props} /> : <User {...props} />; | |
}; | |
const App = () => | |
<div className="container"> | |
<div className="row"> | |
<div className="col-xs-12"> | |
<h2 className="text-center">App</h2> | |
{ users.map(renderUsers ) } | |
</div> | |
</div> | |
</div> | |
; | |
ReactDOM.render( | |
<App />, document.getElementById("app-wrap") | |
) |
Thank you
Nice example. Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for sharing