admin 管理员组文章数量: 1086019
I am new to react and taking over a piece of emergency work. I have a ponent which is invoked on the index page and I am trying to push an object property into it.
so there are two methods here of doing this
const TestBundle = ({lang}) => (
<section className='relative-container'>
<div className='row'>
{lang}
</div>
</section>
)
TestBundle .propTypes = {
lang: React.PropTypes.object.isRequired
}
^ this works.. but how do I get this next concept to work properly - when I tried this solution I had all sort of errors.
const TestBundle = (props) => {
const lang = props.lang
<section className='relative-container'>
<div className='row'>
{lang}
</div>
</section>
}
//different example
const TestBundle = (props) => {
const lang = props.lang
<section className='relative-container'>
<div className='row'>
{lang}
</div>
</section>
}
export default TestBundle
-- but this es up with the error
./src/ponents/Services/TestBundle.js
Module build failed: SyntaxError: D:/wamp/www/project/react/src/ponents/Services/TestBundle.js: Unexpected token (5:2)
3 |
4 | const TestBundle= (props) => {
> 5 | const lang = props.lang
| ^
6 |
7 | <section className='relative-container'>
8 |
I am new to react and taking over a piece of emergency work. I have a ponent which is invoked on the index page and I am trying to push an object property into it.
so there are two methods here of doing this
const TestBundle = ({lang}) => (
<section className='relative-container'>
<div className='row'>
{lang}
</div>
</section>
)
TestBundle .propTypes = {
lang: React.PropTypes.object.isRequired
}
^ this works.. but how do I get this next concept to work properly - when I tried this solution I had all sort of errors.
const TestBundle = (props) => {
const lang = props.lang
<section className='relative-container'>
<div className='row'>
{lang}
</div>
</section>
}
//different example
const TestBundle = (props) => {
const lang = props.lang
<section className='relative-container'>
<div className='row'>
{lang}
</div>
</section>
}
export default TestBundle
-- but this es up with the error
./src/ponents/Services/TestBundle.js
Module build failed: SyntaxError: D:/wamp/www/project/react/src/ponents/Services/TestBundle.js: Unexpected token (5:2)
3 |
4 | const TestBundle= (props) => {
> 5 | const lang = props.lang
| ^
6 |
7 | <section className='relative-container'>
8 |
Share
Improve this question
edited May 11, 2017 at 22:21
The Old County
asked May 11, 2017 at 21:48
The Old CountyThe Old County
13914 gold badges67 silver badges142 bronze badges
5
- You can't put JSX in a middle of a block! – Andrew Li Commented May 11, 2017 at 21:50
-
1
return <section ....;
– Felix Kling Commented May 11, 2017 at 21:51 - Please demonstrate with some answers. @Felix Kling - you just propose smacking a return at the top of section? – The Old County Commented May 11, 2017 at 21:53
- Yes. The function needs to return something. – Felix Kling Commented May 11, 2017 at 21:54
- yeah was checking on the syntax -- now what is the difference between these two methods - whats the best one to use -- why is it ok one way - the other way needs a return - you said JSX - what's the indicator of this. – The Old County Commented May 11, 2017 at 21:56
1 Answer
Reset to default 5Simple fix; you need to add a return statement for your JSX. As it stands you're not returning anything and you are mixing JSX with your constant definition:
const TestBundle = (props) => {
const lang = props.lang;
return (
<section className='relative-container'>
<div className='row'>
{lang}
</div>
</section>
);
}
Or, if you prefer a slightly shorter syntax:
const TestBundle = (props) => <section className='relative-container'>
<div className='row'>
{props.lang}
</div>
</section>
本文标签: javascriptReactJS const passing propertiesStack Overflow
版权声明:本文标题:javascript - reactjs, const, passing properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743949385a2507568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论