Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                body {
  height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
}
.box {
  width: 300px;
  h1 {
    font-size: 20px;
    margin: 0 0 1rem 0;
  }
}

              
            
!

JS

              
                const { createStore, bindActionCreators } = Redux;
const { Provider, connect } = ReactRedux;

// 創建資料庫
const initialState = [
  {
    name: "Winnie",
    id: uuid.v4()
  },
  {
    name: "Mary",
    id: uuid.v4()
  },
  {
    name: "Angel",
    id: uuid.v4()
  }
];

// reducer(初始資料狀態,要對state執行的動作){}
const reducer = (state = {}, action) => {
  switch (action.type) {
    case "ADD_CONTACT":
      const { name } = action;
      return [{ name }, ...state];
    default:
      return state;
  }
}

// 要對state執行的動作
const actions = {
  addContact: name => {
    return {
      type: "ADD_CONTACT",
      id: uuid.v4(),
      name
    };
  }
};

const mapStateToProps = state => {
    return {
      contacts: state
    };
}

const mapDispatchToProps = dispatch => {
    return bindActionCreators(actions, dispatch);
}

// 創建 store : 整合reducer
const store = createStore(reducer, initialState);


class App extends React.Component {
  render() {
    return (
      <section className="section">
        <h1 className="title">Contacts</h1>
        <AddContact addContact={this.props.addContact}/>
        <Contacts
          contacts={this.props.contacts}
        />
      </section>
    );
  }
}

// AddContact包含從store取得的資料
class AddContact extends React.Component {
  constructor(props) {
    // calls the parent constructor(= React.Component).
    super(props);  
    // 產生一個可以儲存 textInput DOM element 的 ref
    this.textInput = React.createRef();
  }
  handleSubmit = (event) => {
    event.preventDefault();
    // 利用「current」來取得 DOM 節點
 this.props.addContact(this.textInput.current.value);
  };
  render() {
    return (
      <div className="box">
          <form onSubmit={this.handleSubmit}> 
            <div className="field">
            <label className="label">Name</label>
            <div className="control">
              <input
                className="input"
                type="text"
                ref={this.textInput}
                placeholder="John Doe"
              />
            </div>
            </div>
            <button type="submit" className="button">
              Add contact
            </button>
          </form>
      </div>
    );
  }
}


// 建立Component
const Contacts = (props) => {
    return (
      <ul>
        {props.contacts.map((contact, id) => (
          <div key={id} className="box">
            <p>{contact.name}</p>
          </div>
        ))}
      </ul>
    );
}

// AppContainer接收connect處理好 mapStateToProps, App的結果
const AppContainer = connect(mapStateToProps,mapDispatchToProps)(App);


// 根據需求單將資料流進 component
ReactDOM.render(
  <Provider store={store}>
    <AppContainer />
  </Provider>,
  document.getElementById("root")
);

              
            
!
999px

Console