gpt4 book ai didi

reactjs - React 上下文和 Next JS

转载 作者:行者123 更新时间:2023-12-03 13:45:27 25 4
gpt4 key购买 nike

我正在尝试向我的应用程序添加简单的 React Context。我在“./components/DataProvider.js”中创建上下文,如下所示:

import React, { Component } from 'react'

const DataContext = React.createContext()

class DataProvider extends Component {
state = {
isAddButtonClicked: false
}

changeAddButtonState = () => {
if( this.state.isAddButtonClicked ) {
this.setState({
isAddButtonClicked: false
})
} else {
this.setState({
isAddButtonClicked: true
})
}
}

render() {
return(
<DataContext.Provider
value={{
isAddButtonClicked: this.state.isAddButtonClicked,
changeAddButtonState: () => {
if( this.state.isAddButtonClicked ) {
this.setState({
isAddButtonClicked: false
})
} else {
this.setState({
isAddButtonClicked: true
})
}
}
}}
>
{this.props.children}
</DataContext.Provider>
)
}

}

const DataConsumer = DataContext.Consumer

export default DataProvider
export { DataConsumer }

然后我将其添加到“./pages/_app.js”

import App, { Container } from 'next/app'

import DataProvider from '../components/DataProvider'

class MyApp extends App {
render () {
const { Component, pageProps } = this.props
return (
<Container>
<DataProvider>
<Component {...pageProps} />
</DataProvider>
</Container>
)
}
}

export default MyApp

并在“./components/AddPostButton.js”中使用它。

import React, {Component} from 'react'
import { DataConsumer } from './DataProvider'

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faPlus } from '@fortawesome/free-solid-svg-icons'

class AddPostButton extends Component {
render() {
return (
<div>
<DataConsumer>
{({ changeAddButtonState }) => (
<a onClick={changeAddButtonState}>
<FontAwesomeIcon icon={faPlus} color='#fff' />
</a>
)}
</DataConsumer>
</div>

)
}

}

export default AddPostButton

但我收到此错误“无法读取未定义的属性“changeAddButtonState””。我正在使用 React 16.7 和 NextJS 7.0.2。不知道出了什么问题。

第二个问题是我应该对所有内容使用一个 Context 还是仅将它们用作 MVC 模式中的模型?

最佳答案

我通过将changeAddButtonState移动到上下文组件状态来修复它,所以我的DataProvider.js现在看起来像这样

import React, { Component } from 'react'

const DataContext = React.createContext()

class DataProvider extends Component {
state = {
isAddButtonClicked: false,
changeAddButtonState: () => {
if (this.state.isAddButtonClicked) {
this.setState({
isAddButtonClicked: false
})
} else {
this.setState({
isAddButtonClicked: true
})
}
}
}

render() {
return(
<DataContext.Provider
value={this.state}
>
{this.props.children}
</DataContext.Provider>
)
}

}

const DataConsumer = DataContext.Consumer

export default DataProvider
export { DataConsumer }

然后在 AddButton 组件中我将代码更改为如下所示

import React, {Component} from 'react'
import { DataConsumer } from './DataProvider'

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faPlus } from '@fortawesome/free-solid-svg-icons'

class AddPostButton extends Component {
constructor(props) {
super(props)
}

render() {
return (
<div>
<DataConsumer>
{(context) => (
<a onClick={context.changeAddButtonState}>
<FontAwesomeIcon icon={faPlus} color='#fff' />
</a>
)}
</DataConsumer>
</div>

)
}

}

export default AddPostButton

关于reactjs - React 上下文和 Next JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54354634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com