简体   繁体   English

如何将 reactjs 中的常量和 useEffect 从 function 更改为渲染组件

[英]How to change constants and useEffect in reactjs from a function to a render component

I have this code in a function我在 function 中有这个代码

function Lobby () {

but I wanted to pass it on to class Lobby extends Component { for a component with但我想将它传递给class Lobby extends Component {用于具有

render () {
    return (

but putting these const and useEffect ect.. gives me an error, is there any way to "translate" this into a component?但是把这些 const 和 useEffect 等..给我一个错误,有没有办法将它“翻译”成一个组件?

I thank those who can help me我感谢那些可以帮助我的人

original code:原始代码:

   function Lobby () {
    const [message, updateMessage] = useState('')
    const [messages, updateMessages] = useState([])

   

 useEffect(() => {
        const handleNewMessage = newMessage =>
            updateMessages([...messages, newMessage])
        socket.on('chat.message', handleNewMessage)
        return () => socket.off('chat.message', handleNewMessage)
    }, [messages])

   

 const handleFormSubmit = event => {
        event.preventDefault()
        if (message.trim()) {
            socket.emit('chat.message', {
                userid: myId,
                lobby,
                username: username,
                message
            })
            updateMessage('') 
            
const recibesound = document.getElementsByClassName("audio-element")[0]
        recibesound.play()
        
   

 }

}

const handleInputChange = event =>
updateMessage(event.target.value)

////teste
const handleTesteChange = event =>
updateMessage(event.target.value)

const handleTesteSubmit = event => {
    event.preventDefault()
    if (message.trim()) {
        socket.emit('chat.message', {
            userid: myId,
            message
            
   

 })
    updateMessage('') 
   
}

}
///teste



  

  return(  <div>
     //html
    </div>
)}
    
 

   export default Lobby;

goal:目标:

class Lobby extends Component {

// same code


 render() {
   return(

<div>
 //html
</div>

 );
  }
}

From understanding you are attempting to convert your hooks function component to a class component.根据您的理解,您正在尝试将您的钩子 function 组件转换为 class 组件。 You cannot use any of your hooks functions in a class component.您不能在 class 组件中使用任何挂钩函数。 You have to rewrite your class component using this.setState() instead of the useState hook (instead of updateMessage, and updateMessages) and this.state.message instead of 'message', and using the componentDidUpdate method instead of the useEffect hook.您必须使用this.setState()而不是 useState 钩子(而不是 updateMessage 和 updateMessages)和this.state.message而不是“消息”,并使用componentDidUpdate方法而不是 useEffect 钩子来重写您的 class 组件。 For your functions (handleInputChange), you can define them as method for your class.对于您的函数 (handleInputChange),您可以将它们定义为 class 的方法。 For your constant (recibesound), you can define it outside your class.对于您的常量(recibesound),您可以在 class 之外定义它。

In short, you have to rewrite your component.简而言之,你必须重写你的组件。 I recommend viewing a React tutorial on class components.我建议查看有关 class 组件的 React 教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 useEffect() 中定义的 function 传递给 reactjs 中的子组件? - How to pass a function defined in useEffect() to a child component in reactjs? 每次渲染时都将不同的 function 传递给 useEffect? - ReactJS 使用效果 - Different function gets passed to useEffect on every render? - ReactJS useEffect 如何在状态更改时重新渲染子组件(父级)reactjs - how to re-render child component on state change(Parent) reactjs 当 State 在 ReactJS 中的不同 .js 文件的组件中发生更改时,如何更新我的 useEffect 挂钩? - How to update my useEffect hook when State change happen in a different .js file's component in ReactJS? 如何使用 useEffect 钩子渲染加载组件? - How to render a loading component with the useEffect hook? 如何从另一个js页面上渲染组件reactjs? - How to render component from another js page reactjs? 如何仅将父组件中的选择性元素呈现给子组件? ReactJs - How to render only selective elements from a Parent Component to child ? ReactJs 如何从文件中获取JSON并将其呈现给reactjs中的组件 - How get JSON from file and render it to component in reactjs 如何从reactJs中的另一个组件调用函数 - How to call function from another component in reactJs 由另一个 function 组件渲染一个 JSX 部件 - reactJS - render a part JSX by another function component - reactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM