Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React analysis #9

Open
ZhangCheng-zh opened this issue Aug 4, 2022 · 0 comments
Open

React analysis #9

ZhangCheng-zh opened this issue Aug 4, 2022 · 0 comments

Comments

@ZhangCheng-zh
Copy link
Owner

step

  • schedule(scheduler)
  • render(reconciler)
  • commit(platform related)

key details

diff

hooks

connect react basic logic to view

view
life cycle function(wrap hooks)
   hooks
react basic runtime

Build my own react

createElement and render function

const Didact = {
    createElement,
    render
}


function createElement(type, props, ...children) {
    return {
        type,
        props: {
            ...props,
            children: children.map(child =>
                typeof child === 'object'
                ? child
                : createTextElement(child))
        }
    }
}

function createTextElement(text) {
    return {
        type: 'TEXT_ELEMENT',
        props: {
            nodeValue: text,
            children: []
        }
    }
}

function render(element, container) {
    const dom = element.type === 'TEXT_ELEMENT' ? element.createTextElement('') : document.createElement(element)
    element.props.children.forEach(child => render(child, dom))

    // add props to element
    const isProperty = key => key !== 'children'
    Object.keys(element.props)
    .filter(isProperty)
    forEach(name => dom[name] = element.props[name])

    container.appendChild(dom)
}



/** @jsx Didact.createElement */
const element = (
    <div id='foo'>
        <a>bar</a>
        <b />
    </div>
)

const container = document.getElementById('root')
ReactDOM.render(element, container)

Step 3: curcurrent mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant