[React]Props
blueMessage.jsx
export const blueMessage = (props) => { console.log(props); const contentStyle = { color : props.color, fontSize : "20px" }; return <p style={contentStyle}>{props.message}</p> };
App.jsx
import { blueMessage } from "./components/blueMessage"; export const App = () => { const onClickButton = () => { alert(); }; const contentStyle = { color : "blue", fontSize : "20px" }; return ( <> <h2 style={{ color:"red" }}>Hello</h2> <p style={contentStyle}>World</p> <blueMessage color="green" message="Great" /> <blueMessage color="red" message="Bad" /> <button onClick={onClickButton}>button</button> </> ); }
childrenを使う場合
blueMessage.jsx
export const blueMessage = (props) => { console.log(props); const contentStyle = { color : props.color, fontSize : "20px" }; return <p style={contentStyle}>{props.children}</p> };
App.jsx
import { blueMessage } from "./components/blueMessage"; export const App = () => { const onClickButton = () => { alert(); }; const contentStyle = { color : "blue", fontSize : "20px" }; return ( <> <h2 style={{ color:"red" }}>Hello</h2> <p style={contentStyle}>World</p> <blueMessage color="green">Great</blueMessage> <blueMessage color="red">Bad</blueMessage> <button onClick={onClickButton}>button</button> </> ); }