GithubHelp home page GithubHelp logo

escapiststupor / arthur-interview Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 3 KB

Created with CodeSandbox

Home Page: https://codesandbox.io/s/github/escapiststupor/arthur-interview

HTML 48.68% JavaScript 51.32%

arthur-interview's Introduction

Counter Context

問題描述

目前小明使用 Context 來實作兩個組件 CountIncreaseButton

  • Count: 用來顯示目前 count 的值
  • IncreaseButton: 用來增加目前 count 的值, 每次往上加 1

但小明發現明明只有 count 的值改變, IncreaseButton 卻依然會重新渲染

請試著實作一個 createContext 函式, 會回傳 ProvderuseContextSelector 來幫助小明解決問題!

const createContext = (defaultValue) => {
  /**
   * Create a react context with given value
   * @param props.value
   * @param props.chidlren
   */
  const Provder = ({ value, children }) => {};

  /**
   * @param selector select value from context
   */
  const useContextSelector = (selector) => {};

  return {
    Provder,
    useContextSelector
  };
};

export default createContext;

Usage

import * as React from "react";
import createContext from "./createContext";

const { Provder, useContextSelector } = createContext();

const Count = () => {
  const count = useContextSelector((ctx) => ctx.count);

  return <div>Count: {count}</div>;
};

const App = () => {
  const [count, setCount] = React.useState(count);
  const value = {
    count,
    setCount
  };

  return <Provder value={value}>...</Provder>;
};

arthur-interview's People

Contributors

escapiststupor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.