【问题标题】:can we write render inside a function component in react?我们可以在反应的功能组件中编写渲染吗?
【发布时间】:2021-05-15 00:21:43
【问题描述】:

render(){
        return (<div>
            <form>
          
            <input type='textarea' placeholder='enter city'/>
            <br/>
            <div className='temp'>
            <input className='Fahren' type='radio'/>
            <label for='temp'>Fahrenheit</label>
            <input className='Celc' type='radio'/>
            <label for='temp'>Celcuis</label>
            </div>
            <br></br>
           <Search/>
            <button type='submit'>Get Forecast</button>
            </form>
    
            </div>);
}
};

    export default App;

上面的代码在我写返回时工作正常(带有整个代码)但是当我尝试使用渲染时它抛出一个错误“语法错误:/Users/Deeksha/Documents/reactandnodejs/weather-app/src/Components/App .js:缺少分号 (7:8)"

【问题讨论】:

  • 函数组件中不需要渲染函数。直接返回你的jsx。
  • render 仅用于类组件。
  • 全部在文档中:reactjs.org/docs/…

标签: reactjs components


【解决方案1】:

难道你的函数不应该用适当的函数构造调用 App 吗?

function App (){
        return (<div>
            <form>
          
            <input type='textarea' placeholder='enter city'/>
            <br/>
            <div className='temp'>
            <input className='Fahren' type='radio'/>
            <label for='temp'>Fahrenheit</label>
            <input className='Celc' type='radio'/>
            <label for='temp'>Celcuis</label>
            </div>
            <br></br>
           <Search/>
            <button type='submit'>Get Forecast</button>
            </form>
    
            </div>);
}

export default App;

或

const App = props => (
   <div>
            <form>
          
            <input type='textarea' placeholder='enter city'/>
            <br/>
            <div className='temp'>
            <input className='Fahren' type='radio'/>
            <label for='temp'>Fahrenheit</label>
            <input className='Celc' type='radio'/>
            <label for='temp'>Celcuis</label>
            </div>
            <br></br>
           <Search/>
            <button type='submit'>Get Forecast</button>
            </form>
    
       </div>);

    export default App;

在我看来像是语法错误

【讨论】:

    猜你喜欢
    • 2020-03-14
    • 2021-05-13
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2018-04-20
    • 2020-12-07
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多