Giter VIP home page Giter VIP logo

reactpatterns-website's Introduction

Website

This website is built using Docusaurus 2, a modern static website generator.

Installation

yarn install

Local Development

yarn start

This command starts a local development server and open up a browser window. Most changes are reflected live without having to restart the server.

Build

yarn build

This command generates static content into the build directory and can be served using any static contents hosting service.

reactpatterns-website's People

Contributors

bunlong avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

reactpatterns-website's Issues

Enhancement to Function as Prop Component

The initial text is understandable, but I recommend a different approach:

Replace

Exactly passing a render callback function to a component is not the issue. The issue is the function as child component implementation chose to use the prop children.

So how could you pass a render callback function to a component in a clean manner?

You would need to name your prop meaningful.

with

Passing a render callback using the function as child component pattern is very common. But that approach is limited, in that there is only one 'children' property in the component. A cleaner and more versatile approach is to pass functions to the component as named props.

This is how common element events are handled, with props-as-attributes like onclick, and onchange, and onsubmit.

Here is an example with a named property passing a function in-line to the Foo component:

...

Suggestions for stateless-function.md

ref:
https://github.com/reactpatterns/reactpatterns-website/blob/main/docs/stateless-function.md

First Suggestion

I recommend a revision of this example

import PropTypes from 'prop-types'

const UserPassword = props => <p>The user password is: {this.props.userpassword}</p>

UserPassword.propTypes = {
  userpassword: PropTypes.string.isRequired,
}

Username.defaultProps = {
  username: 'Jonh',
}

to:

import PropTypes from 'prop-types'

const UserName = props => <p>The user name is: {this.props.username}</p>

UserName.propTypes = {
  username: PropTypes.string.isRequired,
}

UserName.defaultProps = {
  username: 'Jonh',
}

Reasons

  1. In the original, we see a default for username but we don't see where it's used. In the revision it's clear how the username is used.
  2. In the original, UserPassword is PascalCased, but Username is a single word. In the revision there is only one function, and it's consistently PascalCased.
  3. It's not practical to display a password. It is practical to display a name.

Second Suggestion

Regarding this example:

import PropTypes from 'prop-types'

const UserPassword = (props, context) => <p>User password is {context.password}</p>

WowComponent.contextTypes = {
  password: PropTypes.string.isRequired,
}

Concerns:

  1. contextTypes is from the legacy API.
  2. WowComponent isn't used in these examples - this example should include a reference to UserName.
  3. The RefOrContext parameter (second 'context' param on function) should probably be replaced with useContext.
  4. I believe PropTypes.shape should be used to describe context.

Since there are multiple ways of approaching changes to this example, I'm not providing a suggestion.

Thanks!

Enhancement to Accessing a child component

ref: https://github.com/reactpatterns/reactpatterns-website/blob/main/docs/accessing-a-child-component.md

Please consider adding this text to help explain everything that's going on in the example:


Explanation

In SignInModal, render is executed. The Input component's ref attribute is executed as a function, accepting the current component, which is the Input component itself. The function does not return a value. It uses this, which is scoped to SignInModel to create a new property, InputComponent, and it assigns the Input component to that new property.

When render() executes, the new Input component is created, with its this.textInput assigned the result of createRef(). The component render() is executed and the input element's ref attribute is assigned the new reference. Therefore, in Input, textInput refers to the input element as a child component.

Back in SignInModal, in componentDidMount, this.InputComponent is available, as created in the render. The focus() method is executed on that property, which is the Input component. The focus() method uses its this.textInput property, a reference to the 'input' element, to set the element's focus.


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.