gpt4 book ai didi

reactjs - React Context API + withRouter - 我们可以一起使用它们吗?

转载 作者:行者123 更新时间:2023-12-03 14:18:08 25 4
gpt4 key购买 nike

我构建了一个大型应用程序,其中导航栏上的单个按钮打开一个模式。

我正在使用上下文 API 跟踪 modalOpen 状态。

因此,用户单击导航栏上的按钮。模态打开。 Modal 有一个名为 QuoteCalculator 的容器。

报价计算器如下所示:

class QuoteCalculator extends React.Component {
static contextType = ModalContext;
// ...

onSubmit = () => {
// ...
this.context.toggleModal();
this.props.history.push('/quote');
// ..
};

render() {
//...
return(<Question {...props} next={this.onSubmit} />;)
}
}

export default withRouter(QuoteCalculator);

现在,一切都按预期进行。当用户提交时,我会转到正确的路线。我只是在控制台上看到以下警告

index.js:1446 Warning: withRouter(QuoteCalculator): Function components do not support contextType.

我很想忽略这个警告,但我认为这不是一个好主意。

我尝试使用重定向。所以类似

QuoteCalculator looks as follows:

class QuoteCalculator extends React.Component {
static contextType = ModalContext;
// ...

onSubmit = () => {
// ...
this.context.toggleModal();
this.setState({done: true});
// ..
};

render() {
let toDisplay;
if(this.state.done) {
toDisplay = <Redirect to="/quote"/>
} else {
toDipslay = <Question {...props} next={this.onSubmit} />;
}
return(<>{toDisplay}</>)
}
}

export default QuoteCalculator;

这种方法的问题是我不断收到错误

You tried to redirect to the same route you're currently on

另外,我不想使用这种方法,因为那样我就必须撤消完成状态(否则当用户再次单击按钮时,完成为 true,我们将被重定向)...

知道withRouter和history.push发生了什么吗?

这是我的应用

class App extends Component {
render() {
return (
<Layout>
<Switch>
<Route path="/quote" component={Quote} />
<Route path="/pricing" component={Pricing} />
<Route path="/about" component={About} />
<Route path="/faq" component={FAQ} />
<Route path="/" exact component={Home} />
<Redirect to="/" />
</Switch>
</Layout>
);
}
}

最佳答案

警告来源

与大多数高阶组件不同,withRouter将您传递的组件包装在功能组件而不是类组件中。但它仍然在调用hoistStatics ,这将占用您的 contextType static 并将其移动到 withRouter 返回的函数组件。这通常应该没问题,但您发现了一个情况并非如此。您可以查看仓库code了解更多详细信息,但它很短,所以我将在此处为您删除相关行:

function withRouter(Component) {
// this is a functional component
const C = props => {
const { wrappedComponentRef, ...remainingProps } = props;

return (
<Route
children={routeComponentProps => (
<Component
{...remainingProps}
{...routeComponentProps}
ref={wrappedComponentRef}
/>
)}
/>
);
};

// ...
// hoistStatics moves statics from Component to C
return hoistStatics(C, Component);
}

它确实不应该对任何事情产生负面影响。您的上下文仍然有效,只是在从 withRouter 返回的包装组件上被忽略。 。然而,改变一些东西来消除这个问题并不困难。

可能的解决方案

最简单

因为您的模态中需要的是 history.push ,您可以将其作为来自模式父组件的 prop 传递。鉴于您描述的设置,我猜测模式包含在应用程序中的一个位置,在组件树中相当高的位置。如果包含模态框的组件已经是 Route组件,那么它就可以访问history并且可以通过push沿着模态。如果不是,则将父组件包装在 withRouter 中访问路由器 Prop 。

不错

您还可以使用ModalContext.Consumer使模态组件成为模态内容/功能的简单包装器。组件将所需的上下文作为 props 向下传递,而不是使用 contextType .

const Modal = () => (
<ModalContext.Consumer>
{value => <ModalContent {...value} />}
</ModalContext.Consumer>
)

class ModalContent extends React.Component {
onSubmit = () => {
// ...
this.props.toggleModal()
this.props.history.push('/quote')
// ..
}

// ...
}

关于reactjs - React Context API + withRouter - 我们可以一起使用它们吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53973720/

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