How to make this JSX code more generic to avoid code reuse in React?React JSX: Access Props in QuotesReact JSX: selecting “selected” on selected <select> optionLoop inside React JSXReact “after render” code?How to push to History in React Router v4?How to reuse JSX elements in react?Set tabindex to -1 for sortable columnsBuilding a react table using divsProps vs Children to pass HTML content in ReactJShow to style react library components
Why is "la Gestapo" feminine?
Trouble reading roman numeral notation with flats
Exposing a company lying about themselves in a tightly knit industry (videogames) : Is my career at risk on the long run?
Can a Knock spell open the door to Mordenkainen's Magnificent Mansion?
Put the phone down / Put down the phone
Recursively move files within sub directories
Weird lines in Microsoft Word
Make a Bowl of Alphabet Soup
Friend wants my recommendation but I don't want to give it to him
Pre-Employment Background Check With Consent For Future Checks
I keep switching characters, how do I stop?
What is it called when someone votes for an option that's not their first choice?
How to test the sharpness of a knife?
Started in 1987 vs. Starting in 1987
Does capillary rise violate hydrostatic paradox?
Why does a 97 / 92 key piano exist by Bosendorfer?
Strange behavior in TikZ draw command
Magnifying glass in hyperbolic space
Why can't I get pgrep output right to variable on bash script?
How can a new country break out from a developed country without war?
Is this saw blade faulty?
How to preserve electronics (computers, ipads, phones) for hundreds of years?
Offset in split text content
Is there any common country to visit for persons holding UK and Schengen visas?
How to make this JSX code more generic to avoid code reuse in React?
React JSX: Access Props in QuotesReact JSX: selecting “selected” on selected <select> optionLoop inside React JSXReact “after render” code?How to push to History in React Router v4?How to reuse JSX elements in react?Set tabindex to -1 for sortable columnsBuilding a react table using divsProps vs Children to pass HTML content in ReactJShow to style react library components
I'm pretty new to react and I had a question. I have some code that populates some tabs with some info and I wanted some help creating a function that can be reused multiple times instead of reusing the same code for each tab.
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
this.props.raw_strings
</pre>
</div>
</div>
</div>
I was thinking I could create a populateTabs function that can take the count, average length, and the raw string data from the props as a parameter. The count, avg_length, and raw_strings are different for each tab as they each represent a different string type so I've been reusing this block for every tab despite only changing the 3 variables. What is the best way to cut down on the code reuse in this situation? thanks!
reactjs redux jsx
add a comment |
I'm pretty new to react and I had a question. I have some code that populates some tabs with some info and I wanted some help creating a function that can be reused multiple times instead of reusing the same code for each tab.
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
this.props.raw_strings
</pre>
</div>
</div>
</div>
I was thinking I could create a populateTabs function that can take the count, average length, and the raw string data from the props as a parameter. The count, avg_length, and raw_strings are different for each tab as they each represent a different string type so I've been reusing this block for every tab despite only changing the 3 variables. What is the best way to cut down on the code reuse in this situation? thanks!
reactjs redux jsx
add a comment |
I'm pretty new to react and I had a question. I have some code that populates some tabs with some info and I wanted some help creating a function that can be reused multiple times instead of reusing the same code for each tab.
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
this.props.raw_strings
</pre>
</div>
</div>
</div>
I was thinking I could create a populateTabs function that can take the count, average length, and the raw string data from the props as a parameter. The count, avg_length, and raw_strings are different for each tab as they each represent a different string type so I've been reusing this block for every tab despite only changing the 3 variables. What is the best way to cut down on the code reuse in this situation? thanks!
reactjs redux jsx
I'm pretty new to react and I had a question. I have some code that populates some tabs with some info and I wanted some help creating a function that can be reused multiple times instead of reusing the same code for each tab.
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
this.props.raw_strings
</pre>
</div>
</div>
</div>
I was thinking I could create a populateTabs function that can take the count, average length, and the raw string data from the props as a parameter. The count, avg_length, and raw_strings are different for each tab as they each represent a different string type so I've been reusing this block for every tab despite only changing the 3 variables. What is the best way to cut down on the code reuse in this situation? thanks!
reactjs redux jsx
reactjs redux jsx
asked Mar 7 at 0:44
omaromar
254
254
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
React is all about components, so rather than a normal function, you're better off extracting the common markup into a component, which can actually be a "function component" (as opposed to a "class component").
export function PopulateTab( avgLength, count, rawStrings )
return (<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avgLength</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
rawStrings
</pre>
</div>
</div>
</div>);
if tabsContents
is an array of objects like..
const tabsContents = [
avgLength: 5, count: 8, rawStrings: "foo" ,
avgLength: 6, count: 12, rawStrings: "bar" ,
];
you can use PopulateTab
like so..
import PopulateTab from "./populate-tab";
function Tabs( tabsContents )
return (
<div>
tabsContents.map(
( avgLength, count, rawStrings ) =>
<PopulateTab avgLength=avgLength count=count rawStrings=rawStrings />
)
</div>
);
or, more concisely..
function Tabs( tabsContents )
return (<div>tabsContents.map(props => <PopulateTab ...props />)</div>);
add a comment |
The code can be extracted to a component. In case some parameters are common in some cases, it can be higher-order component that accepts common parameters:
const boxHOC = (count, avg_length) => props => (
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
props.raw_strings
</pre>
</div>
</div>
</div>
);
const OneTwoBox = boxHOC(1, 2);
const ThreeFourBox = boxHOC(3, 4);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034388%2fhow-to-make-this-jsx-code-more-generic-to-avoid-code-reuse-in-react%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
React is all about components, so rather than a normal function, you're better off extracting the common markup into a component, which can actually be a "function component" (as opposed to a "class component").
export function PopulateTab( avgLength, count, rawStrings )
return (<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avgLength</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
rawStrings
</pre>
</div>
</div>
</div>);
if tabsContents
is an array of objects like..
const tabsContents = [
avgLength: 5, count: 8, rawStrings: "foo" ,
avgLength: 6, count: 12, rawStrings: "bar" ,
];
you can use PopulateTab
like so..
import PopulateTab from "./populate-tab";
function Tabs( tabsContents )
return (
<div>
tabsContents.map(
( avgLength, count, rawStrings ) =>
<PopulateTab avgLength=avgLength count=count rawStrings=rawStrings />
)
</div>
);
or, more concisely..
function Tabs( tabsContents )
return (<div>tabsContents.map(props => <PopulateTab ...props />)</div>);
add a comment |
React is all about components, so rather than a normal function, you're better off extracting the common markup into a component, which can actually be a "function component" (as opposed to a "class component").
export function PopulateTab( avgLength, count, rawStrings )
return (<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avgLength</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
rawStrings
</pre>
</div>
</div>
</div>);
if tabsContents
is an array of objects like..
const tabsContents = [
avgLength: 5, count: 8, rawStrings: "foo" ,
avgLength: 6, count: 12, rawStrings: "bar" ,
];
you can use PopulateTab
like so..
import PopulateTab from "./populate-tab";
function Tabs( tabsContents )
return (
<div>
tabsContents.map(
( avgLength, count, rawStrings ) =>
<PopulateTab avgLength=avgLength count=count rawStrings=rawStrings />
)
</div>
);
or, more concisely..
function Tabs( tabsContents )
return (<div>tabsContents.map(props => <PopulateTab ...props />)</div>);
add a comment |
React is all about components, so rather than a normal function, you're better off extracting the common markup into a component, which can actually be a "function component" (as opposed to a "class component").
export function PopulateTab( avgLength, count, rawStrings )
return (<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avgLength</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
rawStrings
</pre>
</div>
</div>
</div>);
if tabsContents
is an array of objects like..
const tabsContents = [
avgLength: 5, count: 8, rawStrings: "foo" ,
avgLength: 6, count: 12, rawStrings: "bar" ,
];
you can use PopulateTab
like so..
import PopulateTab from "./populate-tab";
function Tabs( tabsContents )
return (
<div>
tabsContents.map(
( avgLength, count, rawStrings ) =>
<PopulateTab avgLength=avgLength count=count rawStrings=rawStrings />
)
</div>
);
or, more concisely..
function Tabs( tabsContents )
return (<div>tabsContents.map(props => <PopulateTab ...props />)</div>);
React is all about components, so rather than a normal function, you're better off extracting the common markup into a component, which can actually be a "function component" (as opposed to a "class component").
export function PopulateTab( avgLength, count, rawStrings )
return (<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avgLength</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
rawStrings
</pre>
</div>
</div>
</div>);
if tabsContents
is an array of objects like..
const tabsContents = [
avgLength: 5, count: 8, rawStrings: "foo" ,
avgLength: 6, count: 12, rawStrings: "bar" ,
];
you can use PopulateTab
like so..
import PopulateTab from "./populate-tab";
function Tabs( tabsContents )
return (
<div>
tabsContents.map(
( avgLength, count, rawStrings ) =>
<PopulateTab avgLength=avgLength count=count rawStrings=rawStrings />
)
</div>
);
or, more concisely..
function Tabs( tabsContents )
return (<div>tabsContents.map(props => <PopulateTab ...props />)</div>);
answered Mar 7 at 1:19
lecstorlecstor
4,0761218
4,0761218
add a comment |
add a comment |
The code can be extracted to a component. In case some parameters are common in some cases, it can be higher-order component that accepts common parameters:
const boxHOC = (count, avg_length) => props => (
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
props.raw_strings
</pre>
</div>
</div>
</div>
);
const OneTwoBox = boxHOC(1, 2);
const ThreeFourBox = boxHOC(3, 4);
add a comment |
The code can be extracted to a component. In case some parameters are common in some cases, it can be higher-order component that accepts common parameters:
const boxHOC = (count, avg_length) => props => (
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
props.raw_strings
</pre>
</div>
</div>
</div>
);
const OneTwoBox = boxHOC(1, 2);
const ThreeFourBox = boxHOC(3, 4);
add a comment |
The code can be extracted to a component. In case some parameters are common in some cases, it can be higher-order component that accepts common parameters:
const boxHOC = (count, avg_length) => props => (
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
props.raw_strings
</pre>
</div>
</div>
</div>
);
const OneTwoBox = boxHOC(1, 2);
const ThreeFourBox = boxHOC(3, 4);
The code can be extracted to a component. In case some parameters are common in some cases, it can be higher-order component that accepts common parameters:
const boxHOC = (count, avg_length) => props => (
<div className="box box-default">
<div className="box-header with-border">
<h3 className="box-title">Strings Info</h3>
<div className="key-details">
<dl className="dl-horizontal">
<dt>Count</dt>
<dd>count</dd>
<dt>Average Length</dt>
<dd>avg_length</dd>
</dl>
</div>
</div>
<div className="box-header with-border">
<h3 className="box-title">Strings</h3>
<div>
<pre>
props.raw_strings
</pre>
</div>
</div>
</div>
);
const OneTwoBox = boxHOC(1, 2);
const ThreeFourBox = boxHOC(3, 4);
answered Mar 7 at 1:06
estusestus
76.6k22112233
76.6k22112233
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55034388%2fhow-to-make-this-jsx-code-more-generic-to-avoid-code-reuse-in-react%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown