最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - reactjs, const, passing properties - Stack Overflow

matteradmin8PV0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

Simple 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>
Post a comment

comment list (0)

  1. No comments so far