Pass Argument to Function Reactjs
up vote
1
down vote
favorite
I am building a tab bar using react-mdl
library. I have an activeTab
property in my state object to determine which tab was clicked and render the right information.
I have used an onChange method on the tabs holder to fetch the tab id from user click events and passed the id to my function where the activeTab property will be set from events.
This is what I have done so far:
import React, { Component } from 'react';
import { Tabs, Tab } from 'react-mdl';
import './index.css';
class Projects extends Component {
constructor(props){
super(props);
this.state = {
activeTab: 0
}
}
handleTabChange(tabId){
this.setState({
activeTab: tabId
});
console.log(this.state);
}
render() {
return (
<div className="">
<Tabs
activeTab={this.state.activeTab}
onChange={ () => this.handleTabChange(tabId)}>
<Tab>Android</Tab>
<Tab>Web</Tab>
<Tab>Full Stack</Tab>
</Tabs>
</div>
);
}
}
export default Projects;
but when I build the project it crashes in browser with this error:
./src/components/Projects/Projects.js
Line 28: 'tabId' is not defined no-undef
Search for the keywords to learn more about each error.
How can I fix this? Thank you.
javascript reactjs
add a comment |
up vote
1
down vote
favorite
I am building a tab bar using react-mdl
library. I have an activeTab
property in my state object to determine which tab was clicked and render the right information.
I have used an onChange method on the tabs holder to fetch the tab id from user click events and passed the id to my function where the activeTab property will be set from events.
This is what I have done so far:
import React, { Component } from 'react';
import { Tabs, Tab } from 'react-mdl';
import './index.css';
class Projects extends Component {
constructor(props){
super(props);
this.state = {
activeTab: 0
}
}
handleTabChange(tabId){
this.setState({
activeTab: tabId
});
console.log(this.state);
}
render() {
return (
<div className="">
<Tabs
activeTab={this.state.activeTab}
onChange={ () => this.handleTabChange(tabId)}>
<Tab>Android</Tab>
<Tab>Web</Tab>
<Tab>Full Stack</Tab>
</Tabs>
</div>
);
}
}
export default Projects;
but when I build the project it crashes in browser with this error:
./src/components/Projects/Projects.js
Line 28: 'tabId' is not defined no-undef
Search for the keywords to learn more about each error.
How can I fix this? Thank you.
javascript reactjs
as the error saystabId
is an undefined variable. where did you declare it inside therender
method?
– Sagiv b.g
Nov 7 at 7:59
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am building a tab bar using react-mdl
library. I have an activeTab
property in my state object to determine which tab was clicked and render the right information.
I have used an onChange method on the tabs holder to fetch the tab id from user click events and passed the id to my function where the activeTab property will be set from events.
This is what I have done so far:
import React, { Component } from 'react';
import { Tabs, Tab } from 'react-mdl';
import './index.css';
class Projects extends Component {
constructor(props){
super(props);
this.state = {
activeTab: 0
}
}
handleTabChange(tabId){
this.setState({
activeTab: tabId
});
console.log(this.state);
}
render() {
return (
<div className="">
<Tabs
activeTab={this.state.activeTab}
onChange={ () => this.handleTabChange(tabId)}>
<Tab>Android</Tab>
<Tab>Web</Tab>
<Tab>Full Stack</Tab>
</Tabs>
</div>
);
}
}
export default Projects;
but when I build the project it crashes in browser with this error:
./src/components/Projects/Projects.js
Line 28: 'tabId' is not defined no-undef
Search for the keywords to learn more about each error.
How can I fix this? Thank you.
javascript reactjs
I am building a tab bar using react-mdl
library. I have an activeTab
property in my state object to determine which tab was clicked and render the right information.
I have used an onChange method on the tabs holder to fetch the tab id from user click events and passed the id to my function where the activeTab property will be set from events.
This is what I have done so far:
import React, { Component } from 'react';
import { Tabs, Tab } from 'react-mdl';
import './index.css';
class Projects extends Component {
constructor(props){
super(props);
this.state = {
activeTab: 0
}
}
handleTabChange(tabId){
this.setState({
activeTab: tabId
});
console.log(this.state);
}
render() {
return (
<div className="">
<Tabs
activeTab={this.state.activeTab}
onChange={ () => this.handleTabChange(tabId)}>
<Tab>Android</Tab>
<Tab>Web</Tab>
<Tab>Full Stack</Tab>
</Tabs>
</div>
);
}
}
export default Projects;
but when I build the project it crashes in browser with this error:
./src/components/Projects/Projects.js
Line 28: 'tabId' is not defined no-undef
Search for the keywords to learn more about each error.
How can I fix this? Thank you.
javascript reactjs
javascript reactjs
asked Nov 7 at 7:55
Darth Plagueris
546
546
as the error saystabId
is an undefined variable. where did you declare it inside therender
method?
– Sagiv b.g
Nov 7 at 7:59
add a comment |
as the error saystabId
is an undefined variable. where did you declare it inside therender
method?
– Sagiv b.g
Nov 7 at 7:59
as the error says
tabId
is an undefined variable. where did you declare it inside the render
method?– Sagiv b.g
Nov 7 at 7:59
as the error says
tabId
is an undefined variable. where did you declare it inside the render
method?– Sagiv b.g
Nov 7 at 7:59
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You are almost there. From the react-mdl
examples:
You need to pass the tabId as a parameter to the onChange
binding.
onChange={ (tabId) => this.handleTabChange(tabId)}
Full example:
<div className="demo-tabs">
<Tabs activeTab={this.state.activeTab}
onChange={(tabId) => this.setState({ activeTab: tabId })} ripple>
<Tab>Starks</Tab>
<Tab>Lannisters</Tab>
<Tab>Targaryens</Tab>
</Tabs>
<section>
<div className="content">Content for the tab: {this.state.activeTab}</div>
</section>
</div>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You are almost there. From the react-mdl
examples:
You need to pass the tabId as a parameter to the onChange
binding.
onChange={ (tabId) => this.handleTabChange(tabId)}
Full example:
<div className="demo-tabs">
<Tabs activeTab={this.state.activeTab}
onChange={(tabId) => this.setState({ activeTab: tabId })} ripple>
<Tab>Starks</Tab>
<Tab>Lannisters</Tab>
<Tab>Targaryens</Tab>
</Tabs>
<section>
<div className="content">Content for the tab: {this.state.activeTab}</div>
</section>
</div>
add a comment |
up vote
1
down vote
accepted
You are almost there. From the react-mdl
examples:
You need to pass the tabId as a parameter to the onChange
binding.
onChange={ (tabId) => this.handleTabChange(tabId)}
Full example:
<div className="demo-tabs">
<Tabs activeTab={this.state.activeTab}
onChange={(tabId) => this.setState({ activeTab: tabId })} ripple>
<Tab>Starks</Tab>
<Tab>Lannisters</Tab>
<Tab>Targaryens</Tab>
</Tabs>
<section>
<div className="content">Content for the tab: {this.state.activeTab}</div>
</section>
</div>
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You are almost there. From the react-mdl
examples:
You need to pass the tabId as a parameter to the onChange
binding.
onChange={ (tabId) => this.handleTabChange(tabId)}
Full example:
<div className="demo-tabs">
<Tabs activeTab={this.state.activeTab}
onChange={(tabId) => this.setState({ activeTab: tabId })} ripple>
<Tab>Starks</Tab>
<Tab>Lannisters</Tab>
<Tab>Targaryens</Tab>
</Tabs>
<section>
<div className="content">Content for the tab: {this.state.activeTab}</div>
</section>
</div>
You are almost there. From the react-mdl
examples:
You need to pass the tabId as a parameter to the onChange
binding.
onChange={ (tabId) => this.handleTabChange(tabId)}
Full example:
<div className="demo-tabs">
<Tabs activeTab={this.state.activeTab}
onChange={(tabId) => this.setState({ activeTab: tabId })} ripple>
<Tab>Starks</Tab>
<Tab>Lannisters</Tab>
<Tab>Targaryens</Tab>
</Tabs>
<section>
<div className="content">Content for the tab: {this.state.activeTab}</div>
</section>
</div>
answered Nov 7 at 7:59
dubes
2,65611928
2,65611928
add a comment |
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185408%2fpass-argument-to-function-reactjs%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
as the error says
tabId
is an undefined variable. where did you declare it inside therender
method?– Sagiv b.g
Nov 7 at 7:59