Reputation: 21
I've been using this code
{this.state.display === true ?
this.state.content.map((q) => {
<Content
id={q.id}
key={q.id}
content={q.content}
deleteRow={this.deleteRow()}
/>
})
<AppForm />
inside my return method in React, everything works aslong as I dont add the AppForm,but when I add AppForm it gives me an error: Syntax error: Unexpected token.
Can you please help me? Thanks.
Edited:
I want both Content and AppForm to be shown if the user is logged in (display is true)
Here is my complete render code:
return (
<div>
{this.state.display === true ?
this.state.content.map((q) => {
<Content
id={q.id}
key={q.id}
content={q.content}
deleteRow={this.deleteRow()}
/>
})
<AppForm />
: this.state.display === false ?
<Forms
create={this.createUser}
sign={this.signUser}
/>
: 'Error'
}
</div>
);
Upvotes: 1
Views: 1517
Reputation: 5770
You should post all your code. Based on this I can guess one of two issues:
{this.state.display === true ?
this.state.content.map((q) => {
<Content
id={q.id}
key={q.id}
content={q.content}
deleteRow={this.deleteRow()}
/>
})
<AppForm />
1: ?
is the ternary operator. Those are for if this, then do this, else do this
. For inline conditionals like the one you're writing it may be more appropriate to use {this.state.display === true ? &&
. If you DO want either <Content />
or <AppForm />
depending on the condition do
{this.state.display === true ? (
this.state.content.map((q) => {
<Content
id={q.id}
key={q.id}
content={q.content}
deleteRow={this.deleteRow()}
/>
})
) : (
<AppForm />
)
2: JSX requires all returned elements to be wrapped in one element. This is generally accomplished with an arbitray <div>
tag.
return (
<div>
{this.state.display === true ?
this.state.content.map((q) => {
<Content
id={q.id}
key={q.id}
content={q.content}
deleteRow={this.deleteRow()}
/>
})
<AppForm />
</div>
)
If this helps, great! If not, you'll need to provide more information about your code for us to help.
EDIT: You are using the ternary operator incorrectly
return (
<div>
{this.state.display === true ?
this.state.content.map((q) => {
<Content
id={q.id}
key={q.id}
content={q.content}
deleteRow={this.deleteRow()}
/>
})
:
<Forms
create={this.createUser}
sign={this.signUser}
/>
}
<AppForm />
</div>
);
That code should work ^
Basically, ?
is an implicit if, else statement.
true ?
console.log('true')
:
console.log('false')
If the statement to the left of the ?
is true, then the statement to the left of the :
is evaluated, else the statement to the right of the :
is evaluated. You can't supply a second condition, or give it more than two options. You can nest ternary operators if needed but the syntax should always be condition ? if true do this : if false do this
Upvotes: 1
Reputation: 4008
I took the liberty to rewrite it completely :
render() {
const {
display
content
} = this.state;
let component = (
<Forms
create={this.createUser}
sign={this.signUser}
/>
);
if(display) {
const mappedContent = content.map((q) => {
return (
<Content
id={q.id}
key={q.id}
content={q.content}
deleteRow={this.deleteRow.bind(this)}
/>
);
})
component = [
mappedContent,
<AppForm
key="AppForm"
/>
];
}
return (
<div>
{component}
</div>
);
}
A few things:
And regarding your unexpected token, you were missing a : {something}
statement after your test if display was false.
Upvotes: 1