React.js 重复键和不可达代码错误

huangapple go评论79阅读模式
英文:

React.js Duplicate key and Unreachable code BUGS

问题

你好,我正在学习React.js,现在正在制作一个ChatApp。以下是我的错误和代码。

> ./src/App.js 第10行第7列:重复的键 'currentUsername'
> no-dupe-keys
>
> ./src/components/ChatScreen.jsx 第35行第5列:无法访问的代码
> no-unreachable
>
>
> 搜索关键字以了解有关每个警告的更多信息。要忽略警告,
> 在前一行添加// eslint-disable-next-line。

import React, { Component } from 'react'
import Chatkit from '@pusher/chatkit-client'

class ChatScreen extends Component {
  constructor(props) {
    super(props)
    this.state = {
      currentUser: {}
    }
  }

  componentDidMount() {
    const chatManager = new Chatkit.ChatManager({
      instanceLocator: 'v1:us1:1308c34d-00c3-4257-b1c3-647cf56fd5fa',
      userId: this.props.currentUsername,
      tokenProvider: new Chatkit.TokenProvider({
        url: 'http://localhost:3000/authenticate',
      }),
    })

    chatManager
      .connect()
      .then(currentUser => {
        this.setState({ currentUser })
      })
      .catch(error => console.error('error', error))
  }

  render() {
    return (
      <div>

      </div>
    )
    const styles = {
      container: {
        height: '100vh',
        display: 'flex',
        flexDirection: 'column',
      },
      chatContainer: {
        display: 'flex',
        flex: 1,
      },
      whosOnlineListContainer: {
        width: '300px',
        flex: 'none',
        padding: 20,
        backgroundColor: '#2c303b',
        color: 'white',
      },
      chatListContainer: {
        padding: 20,
        width: '85%',
        display: 'flex',
        flexDirection: 'column',
      },
    }

    return (
      <div style={styles.container}>
        <div style={styles.chatContainer}>
          <aside style={styles.whosOnlineListContainer}>
            <h2>Who's online PLACEHOLDER</h2>
          </aside>
          <section style={styles.chatListContainer}>
            <h2>Chat PLACEHOLDER</h2>
          </section>
        </div>
      </div>
    );
  }
}

export default ChatScreen;

希望这有所帮助!

英文:

Hi I'm learning React.js and I'm making a ChatApp right now.
Here is my bug and codes.

> ./src/App.js Line 10:7: Duplicate key 'currentUsername'
> no-dupe-keys
>
> ./src/components/ChatScreen.jsx Line 35:5: Unreachable code
> no-unreachable
>
>
> Search for the keywords to learn more about each warning.To ignore,
> add // eslint-disable-next-line to the line before.

import React, { Component } from &#39;react&#39;
import Chatkit from &#39;@pusher/chatkit-client&#39;
class ChatScreen extends Component{
constructor(props) {
super(props)
this.state = {
currentUser: {}
}
}
componentDidMount() {
const chatManager = new Chatkit.ChatManager({
instanceLocator: &#39;v1:us1:1308c34d-00c3-4257-b1c3-647cf56fd5fa&#39;,
userId: this.props.currentUsername,
tokenProvider: new Chatkit.TokenProvider({
url: &#39;http://localhost:3000/authenticate&#39;,
}),
})
chatManager
.connect()
.then(currentUser =&gt; {
this.setState({ currentUser })
})
.catch(error =&gt; console.error(&#39;error&#39;, error))
}
render() {
return (
&lt;div&gt;
&lt;/div&gt;
)
const styles = {
container: {
height: &#39;100vh&#39;,
display: &#39;flex&#39;,
flexDirection: &#39;column&#39;,
},
chatContainer: {
display: &#39;flex&#39;,
flex: 1,
},
whosOnlineListContainer: {
width: &#39;300px&#39;,
flex: &#39;none&#39;,
padding: 20,
backgroundColor: &#39;#2c303b&#39;,
color: &#39;white&#39;,
},
chatListContainer: {
padding: 20,
width: &#39;85%&#39;,
display: &#39;flex&#39;,
flexDirection: &#39;column&#39;,
},
}
return (
&lt;div style={styles.container}&gt;
&lt;div style={styles.chatContainer}&gt;
&lt;aside style={styles.whosOnlineListContainer}&gt;
&lt;h2&gt;Who&#39;s online PLACEHOLDER&lt;/h2&gt;
&lt;/aside&gt;
&lt;section style={styles.chatListContainer}&gt;
&lt;h2&gt;Chat PLACEHOLDER&lt;/h2&gt;
&lt;/section&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
}
export default ChatScreen;

Could you help me?

答案1

得分: 0

  • ./src/components/ChatScreen.jsx 第35行:5: 不可达代码 no-unreachable

此lint错误警告您,在render方法的顶部有一个return语句,下面跟着永远不会执行的代码。即,这段代码使函数的其余部分变得无用:

return (
  <div>

  </div>
)
  • 重复的键 'currentUsername' no-dupe-keys

此错误出现在App.js中,您尚未与我们分享它。但它告诉您已经给一个对象添加了两次属性 currentUsername。例如,也许您有类似这样的内容:

const myObject = {
  currentUsername: 'bob',
  currentUsername: 'alice',
}

或者尝试创建一个具有相同问题的组件:

<ChatScreen
  currentUsername="bob"
  currentUsername="alice"
/>
英文:

> ./src/components/ChatScreen.jsx Line 35:5: Unreachable code no-unreachable

This lint error is warning you that you have a return statement at the top of your render method, followed by code that will never execute below it. Ie, this code makes the rest of the function useless:

return (
&lt;div&gt;
&lt;/div&gt;
)

> Duplicate key 'currentUsername' no-dupe-keys

This error is occurring in App.js, which you havn't shared with us. But it's telling you that you've given an object the property currentUsername twice. For example, maybe you have something like:

const myObject = {
currentUsername: &#39;bob&#39;,
currentUsername: &#39;alice&#39;,
}

Or are trying to create a component with the same issue:

&lt;ChatScreen
currentUsername=&quot;bob&quot;
currentUsername=&quot;alice&quot;
/&gt;

答案2

得分: 0

第一警告

> ./src/App.js 第10行:7: 重复的键 'currentUsername' no-dupe-keys

你的状态声明中有一个重复的键

constructor() {
    super();
    this.state = {
      currentUsername: &#39;&#39;, 
      currentUsername: &#39;WhatIsYournameScreen&#39;,
    }
    this.onUsernameSubmitted = this.onUsernameSubmitted.bind(this)
  }

根据你的需求,你需要删除其中一个 currentUsername: &#39;&#39;currentUsername: &#39;WhatIsYournameScreen&#39; 行。

第二警告

> ./src/components/ChatScreen.jsx 第35行:5: 不可到达的代码 no-unreachable

在你的 render 方法中有两个 return 语句。你需要删除第一个。

import React, { Component } from &#39;react&#39;
import Chatkit from &#39;@pusher/chatkit-client&#39;

class ChatScreen extends Component{
  constructor(props) {
    super(props)
    this.state = {
      currentUser: {}
    }
  }

  componentDidMount() {
    const chatManager = new Chatkit.ChatManager({
      instanceLocator: &#39;v1:us1:1308c34d-00c3-4257-b1c3-647cf56fd5fa&#39;,
      userId: this.props.currentUsername,
      tokenProvider: new Chatkit.TokenProvider({
        url: &#39;http://localhost:3000/authenticate&#39;,
      }),
    })

    chatManager
      .connect()
      .then(currentUser =&gt; {
        this.setState({ currentUser })
      })
      .catch(error =&gt; console.error(&#39;error&#39;, error))
  }

  render() {
    return (
      &lt;div style={styles.container}&gt;
        &lt;div style={styles.chatContainer}&gt;
          &lt;aside style={styles.whosOnlineListContainer}&gt;
            &lt;h2&gt;Who&#39;s online PLACEHOLDER&lt;/h2&gt;
          &lt;/aside&gt;
          &lt;section style={styles.chatListContainer}&gt;
            &lt;h2&gt;Chat PLACEHOLDER&lt;/h2&gt;
          &lt;/section&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    );
  }
}

const styles = {
      container: {
        height: &#39;100vh&#39;,
        display: &#39;flex&#39;,
        flexDirection: &#39;column&#39;,
      },
      chatContainer: {
        display: &#39;flex&#39;,
        flex: 1,
      },
      whosOnlineListContainer: {
        width: &#39;300px&#39;,
        flex: &#39;none&#39;,
        padding: 20,
        backgroundColor: &#39;#2c303b&#39;,
        color: &#39;white&#39;,
      },
      chatListContainer: {
        padding: 20,
        width: &#39;85%&#39;,
        display: &#39;flex&#39;,
        flexDirection: &#39;column&#39;,
      },
    }

export default ChatScreen;
英文:

First warning

> ./src/App.js Line 10:7: Duplicate key 'currentUsername' no-dupe-keys

You have a duplicate key in your state declaration

constructor() {
super();
this.state = {
currentUsername: &#39;&#39;, 
currentUsername: &#39;WhatIsYournameScreen&#39;,
}
this.onUsernameSubmitted = this.onUsernameSubmitted.bind(this)
}

You need to remove either currentUsername: &#39;&#39; or currentUsername: &#39;WhatIsYournameScreen&#39; line according to your needs.

Second warning

> ./src/components/ChatScreen.jsx Line 35:5: Unreachable code no-unreachable

You have two return statements inside your render method. You need to delete the first one.

import React, { Component } from &#39;react&#39;
import Chatkit from &#39;@pusher/chatkit-client&#39;
class ChatScreen extends Component{
constructor(props) {
super(props)
this.state = {
currentUser: {}
}
}
componentDidMount() {
const chatManager = new Chatkit.ChatManager({
instanceLocator: &#39;v1:us1:1308c34d-00c3-4257-b1c3-647cf56fd5fa&#39;,
userId: this.props.currentUsername,
tokenProvider: new Chatkit.TokenProvider({
url: &#39;http://localhost:3000/authenticate&#39;,
}),
})
chatManager
.connect()
.then(currentUser =&gt; {
this.setState({ currentUser })
})
.catch(error =&gt; console.error(&#39;error&#39;, error))
}
render() {
return (
&lt;div style={styles.container}&gt;
&lt;div style={styles.chatContainer}&gt;
&lt;aside style={styles.whosOnlineListContainer}&gt;
&lt;h2&gt;Who&#39;s online PLACEHOLDER&lt;/h2&gt;
&lt;/aside&gt;
&lt;section style={styles.chatListContainer}&gt;
&lt;h2&gt;Chat PLACEHOLDER&lt;/h2&gt;
&lt;/section&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
}
const styles = {
container: {
height: &#39;100vh&#39;,
display: &#39;flex&#39;,
flexDirection: &#39;column&#39;,
},
chatContainer: {
display: &#39;flex&#39;,
flex: 1,
},
whosOnlineListContainer: {
width: &#39;300px&#39;,
flex: &#39;none&#39;,
padding: 20,
backgroundColor: &#39;#2c303b&#39;,
color: &#39;white&#39;,
},
chatListContainer: {
padding: 20,
width: &#39;85%&#39;,
display: &#39;flex&#39;,
flexDirection: &#39;column&#39;,
},
}
export default ChatScreen;

答案3

得分: 0

`ChatScreen.jsx`:

```jsx
import React, { Component } from 'react'
import Chatkit from '@pusher/chatkit-client'

class ChatScreen extends Component {
  constructor(props) {
    super(props)
    this.state = {
      currentUser: {}
    }
  }

  componentDidMount() {
    const chatManager = new Chatkit.ChatManager({
      instanceLocator: 'v1:us1:1308c34d-00c3-4257-b1c3-647cf56fd5fa',
      userId: this.props.currentUsername,
      tokenProvider: new Chatkit.TokenProvider({
        url: 'http://localhost:3000/authenticate',
      }),
    })

    chatManager
      .connect()
      .then(currentUser => {
        this.setState({ currentUser })
      })
      .catch(error => console.error('error', error))
  }

  render() {
    const styles = {
      container: {
        height: '100vh',
        display: 'flex',
        flexDirection: 'column',
      },
      chatContainer: {
        display: 'flex',
        flex: 1,
      },
      whosOnlineListContainer: {
        width: '300px',
        flex: 'none',
        padding: 20,
        backgroundColor: '#2c303b',
        color: 'white',
      },
      chatListContainer: {
        padding: 20,
        width: '85%',
        display: 'flex',
        flexDirection: 'column',
      },
    }
    return (
      <div style={styles.container}>
        <div style={styles.chatContainer}>
          <aside style={styles.whosOnlineListContainer}>
            <h2>Who's online PLACEHOLDER</h2>
          </aside>
          <section style={styles.chatListContainer}>
            <h2>Chat PLACEHOLDER</h2>
          </section>
        </div>
      </div>
    );
  }
}

export default ChatScreen;

App.js:

import React, { Component } from 'react'
import UsernameForm from './components/UsernameForm'
import ChatScreen from './components/ChatScreen'

class App extends Component {
  constructor() {
    super();
    this.state = {
      currentUsername: '',
      currentScreen: 'WhatIsYournameScreen',
    }
    this.onUsernameSubmitted = this.onUsernameSubmitted.bind(this)
  }

  onUsernameSubmitted(username) {
    fetch('http://localhost:3000/users', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ username }),
    })
      .then(response => {
        this.setState({
          currentUsername: username,
          currentScreen: 'ChatScreen'
        })
      })
      .catch(error => console.error('error', error))
  }

  render() {
    if (this.state.currentScreen === 'WhatIsYourUsernameScreen') {
      return <UsernameForm onSubmit={this.onUsernameSubmitted} />
    }
    if (this.state.currentScreen === 'ChatScreen') {
      return <ChatScreen currentUsername={this.state.currentUsername} />
    }
    return (
      <div>
        <h1>Hoş geldin</h1>
        <UsernameForm onSubmit={this.onUsernameSubmitted} />
      </div>
    );
  }
}

export default App;
英文:

ChatScreen.jsx:

import React, { Component } from &#39;react&#39;
import Chatkit from &#39;@pusher/chatkit-client&#39;
class ChatScreen extends Component{
constructor(props) {
super(props)
this.state = {
currentUser: {}
}
}
componentDidMount() {
const chatManager = new Chatkit.ChatManager({
instanceLocator: &#39;v1:us1:1308c34d-00c3-4257-b1c3-647cf56fd5fa&#39;,
userId: this.props.currentUsername,
tokenProvider: new Chatkit.TokenProvider({
url: &#39;http://localhost:3000/authenticate&#39;,
}),
})
chatManager
.connect()
.then(currentUser =&gt; {
this.setState({ currentUser })
})
.catch(error =&gt; console.error(&#39;error&#39;, error))
}
render() {
const styles = {
container: {
height: &#39;100vh&#39;,
display: &#39;flex&#39;,
flexDirection: &#39;column&#39;,
},
chatContainer: {
display: &#39;flex&#39;,
flex: 1,
},
whosOnlineListContainer: {
width: &#39;300px&#39;,
flex: &#39;none&#39;,
padding: 20,
backgroundColor: &#39;#2c303b&#39;,
color: &#39;white&#39;,
},
chatListContainer: {
padding: 20,
width: &#39;85%&#39;,
display: &#39;flex&#39;,
flexDirection: &#39;column&#39;,
},
}
return (
&lt;div style={styles.container}&gt;
&lt;div style={styles.chatContainer}&gt;
&lt;aside style={styles.whosOnlineListContainer}&gt;
&lt;h2&gt;Who&#39;s online PLACEHOLDER&lt;/h2&gt;
&lt;/aside&gt;
&lt;section style={styles.chatListContainer}&gt;
&lt;h2&gt;Chat PLACEHOLDER&lt;/h2&gt;
&lt;/section&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
}
export default ChatScreen;

App.js:

import React, { Component } from &#39;react&#39;
import UsernameForm from &#39;./components/UsernameForm&#39;
import ChatScreen from &#39;./components/ChatScreen&#39;
class App extends Component {
constructor() {
super();
this.state = {
currentUsername: &#39;&#39;,
currentScreen: &#39;WhatIsYournameScreen&#39;,
}
this.onUsernameSubmitted = this.onUsernameSubmitted.bind(this)
}
onUsernameSubmitted(username) {
fetch(&#39;http://localhost:3000/users&#39;, {
method: &#39;POST&#39;,
headers: {
&#39;Content-Type&#39;: &#39;application/json&#39;,
},
body: JSON.stringify({ username }),
})
.then(response =&gt; {
this.setState({
currentUsername: username,
currentScreen: &#39;ChatScreen&#39;
})
})
.catch(error =&gt; console.error(&#39;error&#39;, error))
}
render() {
if (this.state.currentScreen === &#39;WhatIsYourUsernameScreen&#39;) {
return &lt;UsernameForm onSubmit={this.onUsernameSubmitted} /&gt;
}
if (this.state.currentScreen === &#39;ChatScreen&#39;) {
return &lt;ChatScreen currentUsername={this.state.currentUsername} /&gt; 
}
return (
&lt;div&gt;
&lt;h1&gt;Hoş geldin&lt;/h1&gt;
&lt;UsernameForm onSubmit={this.onUsernameSubmitted} /&gt;
&lt;/div&gt;
);
}
}
export default App;

huangapple
  • 本文由 发表于 2020年1月6日 02:17:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59602859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定