context
16.x版本的Context
// FileContext.js
import React from 'react';
export const FileContext = React.createContext('fileContext');//main.js
import { FileContext } from './FileContext';
export default class Main extends React.Component {
render() {
const fileName = 'a-file'
<FileContext.Provider value = {fileName} >
// do smt, include Show Component(<Show />)
</FileContext.Provider>
}
}//Show.js
import {FileContext} from './FileContext';
export default class Show extends React.Component {
render() {
<FileContext.Consumer >
{
(fileName) => //do smt, this fileName is from main context
}
</FileContext.Consumer>
}
}旧版的context
Last updated