项目作者: Gurenax

项目描述 :
Hello Context, a simple context api example
高级语言: JavaScript
项目地址: git://github.com/Gurenax/react-context-api.git
创建时间: 2018-05-11T06:40:05Z
项目社区:https://github.com/Gurenax/react-context-api

开源协议:

下载


React Context API

  • The simplest way to test Context API is through Hello World.
  • This app uses context API to store an object of messages and a function to switch between messages.

Creating Hello Context

  • The context consists of an object called message and a function ‘toggleMessage’.
    ```javascript
    import React from ‘react’

// An object called messages
export const messages = {
message1: {
firstWord: ‘hello’,
secondWord: ‘world’
},
message2: {
firstWord: ‘hi’,
secondWord: ‘everyone’
}
}

// The HelloContext itself
// - this needs to match the Consumer props
export const HelloContext = React.createContext({
message: messages.message1, // Message Object
toggleMessage: () => {}, // Empty Function
})

  1. ## The Consumer
  2. - The consumer will be my Message component.
  3. - The Message component will display a message and a button which changes the message.
  4. ```javascript
  5. import React from 'react'
  6. import { HelloContext } from '../contexts/hello-context';
  7. const Message = () => {
  8. return (
  9. <HelloContext.Consumer>
  10. {
  11. ({message, toggleMessage}) => (
  12. <header className="App-header">
  13. <h1 className="App-title">{ message.firstWord + ' ' + message.secondWord }</h1>
  14. <button onClick={ toggleMessage }>Change Message</button>
  15. </header>
  16. )
  17. }
  18. </HelloContext.Consumer>
  19. )
  20. }
  21. export default Message

The Provider

  • The provider will be contained in my App component and its value will equal the state.
  • My state will be initialised to:
    • message: The first message in my message object
    • toggleMessage: The function that will change the message from ‘hello world’ to ‘hi everyone’
      ```javascript
      import React, { Component } from ‘react’
      // import logo from ‘../assets/images/logo.svg’
      import ‘../styles/App.css’

import { HelloContext, messages } from ‘../contexts/hello-context’
import Message from ‘./Message’

class App extends Component {
// This function strictly needs to be declared before initialising the state
_toggleMessage = () => {
console.log(‘Test’)
this.setState(prevState => ({
message:
prevState.message === messages.message1
? messages.message2
: messages.message1
}))
}

state = {
message: messages.message1,
toggleMessage: this._toggleMessage,
}

render() {
return (





)
}
}

export default App
```

Updates

  • Added Button Context - Context that specifies the Title of a Button.
  • Added Action Context - Context that provides sync and async actions to display the message as an alert.

This project was bootstrapped with Create React App.