值未通过 React 上下文传递 API
Value is not passed through the React Context API
我正在尝试使用 React Context 重写我的应用程序 API 但遇到了下一个问题 - 我想通过提供者传递给消费者的值没有传递。
初始状态:
let initialState = {
ve: 'randomValue'
};
export default initialState;
提供商:
import React, {Component} from 'react';
import MyContext from './myContext';
import initialState from './initialState';
class MyProvider extends Component{
constructor(props) {
super(props);
this.state = initialState;
}
render() {
return(
<MyContext.Provider value={{
state: this.state,
}}>
{this.props.children}
</MyContext.Provider>
)
}
};
export default MyProvider;
消费者:
import React, {Component} from 'react';
import {Text, View} from 'react-native';
import MyContext from './Context/myContext';
export default class App extends Component {
render() {
return (
<MyContext.Consumer>
{context => (
<View>
<Text>
{context}
</Text>
</View>
)}
</MyContext.Consumer>
);
}
}
我没有收到任何错误,但也没有上下文值。
您还需要在 MyContext.Consumer
组件之上的某处使用 Provider
组件。
例子
const initialState = {
ve: "randomValue"
};
const MyContext = React.createContext();
class MyProvider extends React.Component {
constructor(props) {
super(props);
this.state = initialState;
}
render() {
return (
<MyContext.Provider
value={{
state: this.state
}}
>
{this.props.children}
</MyContext.Provider>
);
}
}
class App extends React.Component {
render() {
return (
<MyProvider>
<MyContext.Consumer>
{context => <div>{JSON.stringify(context)}</div>}
</MyContext.Consumer>
</MyProvider>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
我正在尝试使用 React Context 重写我的应用程序 API 但遇到了下一个问题 - 我想通过提供者传递给消费者的值没有传递。
初始状态:
let initialState = {
ve: 'randomValue'
};
export default initialState;
提供商:
import React, {Component} from 'react';
import MyContext from './myContext';
import initialState from './initialState';
class MyProvider extends Component{
constructor(props) {
super(props);
this.state = initialState;
}
render() {
return(
<MyContext.Provider value={{
state: this.state,
}}>
{this.props.children}
</MyContext.Provider>
)
}
};
export default MyProvider;
消费者:
import React, {Component} from 'react';
import {Text, View} from 'react-native';
import MyContext from './Context/myContext';
export default class App extends Component {
render() {
return (
<MyContext.Consumer>
{context => (
<View>
<Text>
{context}
</Text>
</View>
)}
</MyContext.Consumer>
);
}
}
我没有收到任何错误,但也没有上下文值。
您还需要在 MyContext.Consumer
组件之上的某处使用 Provider
组件。
例子
const initialState = {
ve: "randomValue"
};
const MyContext = React.createContext();
class MyProvider extends React.Component {
constructor(props) {
super(props);
this.state = initialState;
}
render() {
return (
<MyContext.Provider
value={{
state: this.state
}}
>
{this.props.children}
</MyContext.Provider>
);
}
}
class App extends React.Component {
render() {
return (
<MyProvider>
<MyContext.Consumer>
{context => <div>{JSON.stringify(context)}</div>}
</MyContext.Consumer>
</MyProvider>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>